Computing.Net > Forums > Programming > dynamic arrays in c++

dynamic arrays in c++

Reply to Message Icon

Original Message
Name: rde
Date: February 18, 2003 at 07:11:37 Pacific
Subject: dynamic arrays in c++
OS: win98 se
CPU/Ram: athlon 1800, 256MB
Comment:

hi

I have a problem with dynamic arrays.
In my program, i have to add elements to an existing array.Here is the way im doing this:
Create a temporary array, copy all the elements, delete the old array, create a new array with one element more, copy all elements back from temprary array and finally add the new element.
Seems very complicated and time consuming to me, but I couldn't figure out a better way. Is there anyone who knows how to do it better? Thanks in advance

rde


Code:

int elements=0; //number of elements in array
mytype* myarray;

void addelement(mytype);

void main(){
...
addelement(newelement)
...
}

void addelement(mytype newelement){

mytype *temparray;
temparray = new mytype[elements];

for(int i=0;ielements;i++)
temparray[i] = myarray[i];

delete[] myarray;

myarray = new mytype[elements+1];

for(int i=0;ielements;i++)
myarray[i] = temparray[i];

myarray[elements] = newelement;

delete[] temparray;

elements++;
}


Report Offensive Message For Removal


Response Number 1
Name: rde
Date: February 18, 2003 at 07:16:20 Pacific
Reply: (edit)

the logical operator in the for-statements between i and elements is "i smaller than elements".


Report Offensive Follow Up For Removal

Response Number 2
Name: Don Arnett
Date: February 18, 2003 at 11:38:14 Pacific
Reply: (edit)

I have two thoughts:

1 - instead of adding just one element when you reallocate the array, how about adding 10 elements or 100 elements (whatever number suits the situation best). Then you would cut down the number of times that you will go thru the reallocation process.

2 - look into realloc(). It's a C function but will still work in a C++ program. I don't know if there is a C++ equivalent. realloc() does basically what your are doing with your own code. It reallocates a new chunk of memory, copies the contents to the new chunk and removes the old chunk (more or less).


Report Offensive Follow Up For Removal

Response Number 3
Name: Kaleidoscope
Date: February 20, 2003 at 13:13:48 Pacific
Reply: (edit)

/*
Since operator new returns a pointer to the new allocated block
the use of a temporary array is redundant. The only thing to make sure
is that you delete the old pointer before assigning the new one.

Keep in mind that deleting a NULL pointer won't hurt at all.

*/

#include

typedef double mytype;

mytype *myarray=NULL;
bool addelement(mytype);

void main()
{

using namespace std;

double newelement=0;

for (int i=0; i10; i++)
{
newelement=i;
if(!addelement(newelement))
{
// Handle Error Here
cout"Unable to add element (ie: out of memory?)\n"endl;
}

cout"New Element Added: "myarray[i]endl;
}

delete [] myarray;
}

bool addelement(mytype newelement)
{

static int elements=0;

mytype *newarray = new mytype[elements+1];
if (!newarray) return false;

if (myarray)
for(int i=0;ielements;i++)
newarray[i]=myarray[i];

delete [] myarray;

myarray=newarray;
myarray[elements]=newelement;

elements++;

return true;
}



Report Offensive Follow Up For Removal

Response Number 4
Name: Kaleidoscope
Date: February 20, 2003 at 13:24:54 Pacific
Reply: (edit)

Some more clues that may be usefull to you...

It is also a good idea to make sure that myarray is not NULL before trying to access it.

I preferred to make 'elements' static inside addelement() to make sure that no other code can change it. If you also need to remove elements, it would be a good idea to write a class that owns private data and element count, and public methods to use it.

At this point, you could consider optimizing your class by adding some features, like the one described in the second reply. One last thing to make it even more generic and reusable, would be a template class (which essntially allows you to create and manage arrays containing different data types, with the same code).

By the way, I see this forum hurts my code! :-) Is there any way not to mess things up with 'less than' operators?

Anyway... i10 or ielements is "i less than ..." and #include is the inclusion for iostream.h

Hope this helps!


Report Offensive Follow Up For Removal

Response Number 5
Name: rde
Date: February 21, 2003 at 10:56:42 Pacific
Reply: (edit)

Thanks a lot for your hints. I think this will help me.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: dynamic arrays in c++

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge