Jump to content
  • Advertisement
Sign in to follow this  
31337noob

resize array?

This topic is 4483 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

i have a something called Shape **sPtr = 0; ok when i add new data items to it i do this. sPtr = new Shape*[num+1]; ....... ok, i think every time it finds sPtr is resizing it deletes the old data in that array. my question is how do you keep the data that is in the array when you want to resize something?

Share this post


Link to post
Share on other sites
Advertisement
You are dynamically creating memory on the heap. This will not delete memory previously allocated. Use the delete operator on the pointer first then allocate another array.

With that said, someone else will tell you to use the std::vector if you want an array that will resize.

If you don't use the new operator you can use the realloc command to have the memory pointed to preserved. If you take the new operator approach, then you will just have to memcpy the data over to the new array.

Share this post


Link to post
Share on other sites
Quote:
Original post by 31337noob
expect i cant use vectors because this is for homework.


Homework is supposed to be your assignment to figure out yourself. I think they call it a learning experience.


unsigned int * pIntArray = new unsigned int[20];
unsigned int * pIntArrayResize = new unsigned int[21];
memcpy(pIntArrayResize, pIntArray, sizeof(unsigned int) * 20);
delete [] pIntArray;
pIntArray = pIntArrayResize;




There is a problem with your your statement where you try to assign new char*[num + 1] to sPtr. They are unrelated types.

Share this post


Link to post
Share on other sites
Quote:
Original post by 31337noob
expect i cant use vectors because this is for homework.


never mind, i could.

i used it and it worked.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!