Jump to content
  • Advertisement

Archived

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

SIGMA

Dynamic arrays...

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

Hello, This is probably a pretty stupid question but if I had an int pointer like int *piInt, and I then wanted this int to have 10 elements so I did piInt = new int[10]; everything is fine so far, but what if I decided that somewhere later that I wanted to have my int to have an extra element, so now it''d have a 11, without erasing all previous data? Would I do something like piInt = new int[11] or piInt = new int? Please help! Cheers, -Jesse Vernon

Share this post


Link to post
Share on other sites
Advertisement
No! you have to create a dynamic array class. ..you must always store old values, delete the previous array and use piInt = new int[11] ..and then replace first 10 indives with old data and add 11th element. ...or just use CArray template (CArray). ...or STL''s list, vector or something ... you''ll find more in MSDN.

Dave007
dave007@volny.cz

Share this post


Link to post
Share on other sites
Unfortunately, you can''t do that by just using new and delete. There are a few other ways of doing it, though - you could use std::vector. Or use malloc/realloc/free instead of new/delete.

Share this post


Link to post
Share on other sites
or the really simple answer is....


  
// create an array.

int *pInt = new int[10];


fill the array and do whatever.

ok, now we want to resize the array.

  
// create a new array.

int *pTemp = new int[20];
// copy the old array elements to the new array.

memcpy(pTemp, pInt, (10 * sizeof(int));
// free the old array.

free(pInt);
// assign the variable to the new pointer address.

pInt = pTemp;


that''s all you gotta do.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
sorry, "free" should be "delete". i don''t use "new" and "delete" on pointers so, sorry for my mistake.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
There is also a function provided with the stand c runtime library called realloc( ).

This function can be nasty if you run out of a free memory block large enough to hold the new size, and will have to consitently check for that problem to avoid access violations.

Your other options would be to use vectors (stl has a nice class for these - so I understand, I personally do not use stl), or you could create a linked list and store the information as needed in there.

Now, if you are going to be dealing only with a few bytes of information, esecially in something as simple as a char array, my suggestion would be to use realloc( ), or use a vector. If, however, you intend to be using 2 dimensional arrays, or data structures, I would STRONGLY recommend that you use a linked list.

With a linked list you do not have to worry about finding a block of free memory equal in size to the sum of the objects, but instead will only need a block of memory free and large enough to support each object individually, as one object will tell you where in the memory you can find the next, and vice-versa if you so decide.

Gamedev''s AI Auto-Reply bot.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just a note: realloc only works with malloc/free, not new/delete.

Share this post


Link to post
Share on other sites
Okay thanks, that makes perfect sense, I just wasn''t sure how I should go about doing it, but now I am. And yeah, stl is soooo ''70s and I''m not using malloc. Oh and one other thing, the linked list is complete overkill when it comes to storing ints, linked lists aren''t the solution to everything!! Okay, I''m done,
Thanks,
-Jesse

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!