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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

dynamic arrays in c++

Reply to Message Icon

Name: rde
Date: February 18, 2003 at 07:11:37 Pacific
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++;
}



Sponsored Link
Ads by Google

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

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


0

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

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


0

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

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



0

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

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!


0

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

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

batch to create desktop s... Java - get application pa...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: dynamic arrays in c++

Dynamic Arrays in C www.computing.net/answers/programming/dynamic-arrays-in-c/3396.html

C++ Dynamic Array www.computing.net/answers/programming/c-dynamic-array/11481.html

dynamic arrays in c++ www.computing.net/answers/programming/dynamic-arrays-in-c/6348.html