Expanding array
Hey guys, I need some help here.
I''ve got an array that I want to expand. These are the steps that I can think of in order to do so. Any ideas on how to make this easier without any memory leaks? I would like to just have an array of pointers to copy to instead of all the new/delete business, but I can''t remember how to make an array of empty pointers(if you even can).
oldArray
1. create new array with same size as oldArray
2. delete pointers in newArray
3. copy pointers from oldArray into newArray
4. recreate oldArray with new size
5. delete pointers in oldArray
6. copy pointers from newArray into oldArray
PS Vectors and Linked Lists won''t work, because I need the speedy fast array access times.
Thanks!
CD
Jesus is Lord!!
One of the reasons I don''t want to use a vector is that it''s a 2D array. A vector in a situation like mine would be harmful instead of helpful.
CD
Jesus is Lord!!
CD
Jesus is Lord!!
You must be thinking of something else, then, because a std::vector is basically a 1 dimensional, resizable, quite fast array.
Sorry, worded it incorrectly. I don''t want to use a vector, because the array I need to resize is a 2D array. :]
CD
Jesus is Lord!!
CD
Jesus is Lord!!
std::vector< std::vector< int > > x;
Boom, 2D array.
[edited by - Promit on April 15, 2004 5:32:17 PM]
quote:Original post by CD579
1. create new array with same size as oldArray
2. delete pointers in newArray
3. copy pointers from oldArray into newArray
4. recreate oldArray with new size
5. delete pointers in oldArray
6. copy pointers from newArray into oldArray
You've got some redundancy there. Why not do this:
1. create new array with new size, initialize all pointers to NULL
2. copy the pointers from oldArray into newArray
3. delete [] oldArray, use newArray
// assuming MyClass **oldArray was already declared and set // up, and that numInArray is how many elements are in // oldArray. MyClass **tmpArray; int numNeeded = numInArray + 10; // we want 10 more // allocate a new array tmpArray = new MyClass * [numNeeded]; // copy existing pointers, NULL the rest for(int i=0;i<numNeeded;i++) { if(i < numInOldArray) // first pointers are copied tmpArray[ i ] = oldArray[ i ]; else // the rest are NULLed tmpArray[ i ] = NULL; } // nuke the old array of pointers, we're using the new one delete [] oldArray; oldArray = tmpArray; numInArray = numNeeded;
[edited by - BriTeg on April 15, 2004 5:38:42 PM]
I was thinking of doing that, but when you do this:
tmpArray = new MyClass * [numNeeded];
aren''t you creating numNeeded MyClass''s? Then when you set the pointer to NULL, the memory is still there, but you can''t access it, therefore a memory leak?
At least that''s the way my c++ book explains it. Any thoughts?
CD
Jesus is Lord!!
tmpArray = new MyClass * [numNeeded];
aren''t you creating numNeeded MyClass''s? Then when you set the pointer to NULL, the memory is still there, but you can''t access it, therefore a memory leak?
At least that''s the way my c++ book explains it. Any thoughts?
CD
Jesus is Lord!!
quote:Original post by CD579
I was thinking of doing that, but when you do this:
tmpArray = new MyClass * [numNeeded];
aren''t you creating numNeeded MyClass''s? Then when you set the pointer to NULL, the memory is still there, but you can''t access it, therefore a memory leak?
At least that''s the way my c++ book explains it. Any thoughts?
Either you have a really bad book, or just misinterpreting it. When you do:
tmpArray = new MyClass * [numNeeded];
You are creating an array of pointers to MyClass. Look at the expression like this:
tmpArray = new (MyClass*)[numNeeded];
And you can see that immediatly outside the left bracket is the type, just as usual.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement