Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CD579

Expanding array

This topic is 5270 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!!

Share this post


Link to post
Share on other sites
Advertisement
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!!

Share this post


Link to post
Share on other sites
You must be thinking of something else, then, because a std::vector is basically a 1 dimensional, resizable, quite fast array.

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites

std::vector< std::vector< int > > x;


Boom, 2D array.

[edited by - Promit on April 15, 2004 5:32:17 PM]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!