Expanding array

Started by
16 comments, last by CD579 20 years ago
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!!
Jesus is Lord!!
Advertisement
Using a std::vector would not be any slower than what you are proposing.
std::vector does have speedy array access time. Use it. It''s good. Really.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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!!
Jesus is Lord!!
You must be thinking of something else, then, because a std::vector is basically a 1 dimensional, resizable, quite fast array.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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!!
Jesus is Lord!!
std::vector< std::vector< int > > x;  


Boom, 2D array.

[edited by - Promit on April 15, 2004 5:32:17 PM]
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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]
Brianmiserere nostri Domine miserere nostri
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!!
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