quote: I just had a thought and I wanted to confirm if this is a good way to do this, or if there is a standardized or easier way. If I created an array like: int array[15]; And I wanted to add another element to it. Is there a function in C++ that allows this or can I just use new() to make an array with 16 slots and then memcpy() the old array into the new one and then free() the old one? Just a sudden idea that popped in my head and I want to know if that's coshure or a bad way of doing it. Thanks.============================== "Need more eeenput..." - #5, "Short Circuit" ============================== Edited by - Gaiiden on 11/7/00 12:22:08 PM Edited by - Gaiiden on 11/13/00 3:06:19 PM
Resizing arrays
Hey! I got another question!! Read the bottom post pleaz, thanks!
Drew Sikora
Executive Producer
GameDev.net
quote: Original post by Gaiiden
...
And I wanted to add another element to it. Is there a function in C++ that allows this or can I just use new() to make an array with 16 slots and then memcpy() the old array into the new one and then free() the old one?
You can do it this way but, it''s not really advisable to do this.
If the array is large then you may have problems. Plus if you want to add a bunch of elements you''ll be doing a whole lot of copying.
Possible solutions:
- Make the original array bigger then you will ever need. You''ll always be assured a space.
- Create a linked list to dynamically increase size.
- Use something from the STL like Vector.
-------
Andrew
umm... using C malloc realloc and free would be the squence to use. In C++ according to the standards you should be able to use the new operator to do the same thing, problem is that atleast M$VC 6.0 doesnt implement it correctly.
so either use plain vanilla C for it or use the STL Vector or some linked structure
so either use plain vanilla C for it or use the STL Vector or some linked structure
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Actually STL''s vector basically does what Gaiiden stated before, where it reallocates a new array and copies over the data from the old one. It includes a capacity variable which defaults the array to whichever size you want, and whenever the size is greater than the capacity it increases the array by multiples of 2 (a good compromise between size and speed) and sets capacity to the new size.
You can use STL''s vector or create one yourself fairly easily.
- Houdini
You can use STL''s vector or create one yourself fairly easily.
- Houdini
- Houdini
In your case you defeanately want to use vectors, they are generally superior to plain old arrays and have to functions to do exactly what you want.
Possibility
Possibility
An alternative is segmented arrays. Basically an array or linked list of pointers to arrays of values. i.e.
That lets you grow to x*y without copying anything. When you access an element you have to break it into two steps, i.e. *(*(i + (z / y)) + (z % y)). If you use powers of two the divide and mod can be replace by shift and bitwise and. If you have to expand then you copy the list of pointers which can be relatively small. A straight linked list of small items would be a waste since the pointers will take as much space as the data in the case of integers or twice in the case of small integers. Aside from that random access degenerates into a sequential search on average having to access n/2 elements of the list. Using a linked list for the segments of the array substantially decreases the overhead and the average access. The access reduces to n/(2*rowsize) on average. A much less substantial penality.
int *i[x];i[0] = new int[y];
That lets you grow to x*y without copying anything. When you access an element you have to break it into two steps, i.e. *(*(i + (z / y)) + (z % y)). If you use powers of two the divide and mod can be replace by shift and bitwise and. If you have to expand then you copy the list of pointers which can be relatively small. A straight linked list of small items would be a waste since the pointers will take as much space as the data in the case of integers or twice in the case of small integers. Aside from that random access degenerates into a sequential search on average having to access n/2 elements of the list. Using a linked list for the segments of the array substantially decreases the overhead and the average access. The access reduces to n/(2*rowsize) on average. A much less substantial penality.
Keys to success: Ability, ambition and opportunity.
Needless to say that is excessive for 60 bytes of memory. Using a pointer to an array, allocating a new one, copying the elements, deleting the old one and setting the pointer to point to the new one would most likely work just fine for what you are doing. I would increment the size by more than one though. Doubling can be extremely wasteful with a large array. You will on average waste one half of the increment. Personally I would count the expands and after so many expansions up the increment if I wanted a general purpose compromise. If you really wanted to be elborate you would time the expansion rate and adjust the increment towards a target rate. Oops, there I go again. What you are doing should be fine.
Keys to success: Ability, ambition and opportunity.
OK, I''m recycling this thread instead of posting a new one (your welcome Moderator (whoever you are) ). My new question is that I tried resizing the array as specified above, but you can''t put variables in the brackets [] darn it! So I defined a constant. Say 5. Now say I wanted to double the array. OK, I tried this:
Notice here I was able to put a value into the 125th spot!! How did that happen?? I thought I would get 12 spots (6*2 right (I counted the zero)). What heppened here? Was it the way I multiplied inside the brackets or what?
==============================
"Need more eeenput..."
- #5, "Short Circuit"
==============================
#include #include #include #define size 5int main(){ int five[size]; cout << "Size before enlargement: " << sizeof(five) << endl; int *five = new int[2*size]; five[125] = 7; cout << "Testing: " << five[125] << endl; return 0;}
Notice here I was able to put a value into the 125th spot!! How did that happen?? I thought I would get 12 spots (6*2 right (I counted the zero)). What heppened here? Was it the way I multiplied inside the brackets or what?
==============================
"Need more eeenput..."
- #5, "Short Circuit"
==============================
Drew Sikora
Executive Producer
GameDev.net
oooooh CRAP. Yeah yeah yeah. Put the torches down, I just realized my STUPID mistake. The brackets are the size of the new variable! DOH!!! That doesn''t create an array! Oh man I''m kicking my own ass.... However, if that''s really the case, why can I access a variable in ten[125] when it isn''t even defined as an array? And how can I use the new keyword to create an array?
==============================
"Need more eeenput..."
- #5, "Short Circuit"
==============================
==============================
"Need more eeenput..."
- #5, "Short Circuit"
==============================
Drew Sikora
Executive Producer
GameDev.net
int* array;array = new int[whateverSizeYouWant];
the reason why you are able to access i[125] is that this really converts to the memory located at
(addressOfI + (125*sizeof(int))
which means if the starting address of your array was at 1000
then i[125] points to the memory at 1000+(125*4) or 1625
now most of the time this will probably give you a segmentation fault or other error that will crash your program, but sometimes it will retrieve the data from the four bytes at that location, interpret it as an int, and give you the value (which should be nothing but garbage or could be data allocated to other parts of your program which means changing this data will mess you up bad!!!)
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement