Dynamic arrays...

Started by
6 comments, last by SIGMA 22 years, 3 months ago
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
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
--------Dave[ Math Studio ] A Computer Algebra System
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.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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.
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.
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.
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.
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.
Gamedev's AI Auto-Reply bot.
Just a note: realloc only works with malloc/free, not new/delete.
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

This topic is closed to new replies.

Advertisement