Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 advancerde
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++;
}

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).

/*
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;
}

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!

![]() |
batch to create desktop s...
|
Java - get application pa...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |