increasing array size

Started by
7 comments, last by gbizzle 19 years, 2 months ago
hey everyone, Put simply: when i have an array that becomes full, i want to make it bigger. To do this, I make a second, larger array and copy the elements from the smaller array to the larger one, and THEN make the smaller (old) array reference the larger one, like so: //in some method: Rect tempArray[] = new Rect[2 * mainArray.length]; for (int n = 0; n < mainArray.length; n++) { tempArray[n] = mainArray[n]; } mainArray = tempArray; //end of sample is this the right way to do it? After copying the elements of the mainArray to tempArray, can i just say mainArray = tempArray? thanks
Advertisement
This method will indeed work. But don't forget to release the memory of the previous array.
EDIT: also mind if you are using pointers that you must overload the = operator to copy those pointers to prefend dangling pointers.

Another way is to use the STL vector class. This class implements an dynamic array and is fairly easy to use, if you know what template are ;). Simply put is that you can use the vector class for almost any data type.

As an example:
std::vector<int> intVector;
intVector.push_back (1);
intVector.push_back (2);
assert (intVector[0] == 1);

The code above first declares a new vector object which contents will be int's. Then 2 integers are added to the list. Finally we make sure that the content is added.

Hope this helps a bit.

Crafter 2D: the open source 2D game framework

?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]

that would work but generally if you have a variable amount of data you want to use something like a linked list. I'd suggest either std::list or std::vector from the STL library. they are dynamically sized groupings of data. as you need more space, more is given to you. std::vector acts very much like an array and will keep all of the data contiguous in memory, std::list acts very much like a linked list and you data can become fragmented in memory.

incedentally a better way of copying from array to array is using the memcpy method; it is infinitely faster than looping through the array and copying element by element:

int anArray[5];int aBiggerArray[10];//put the contents of anArray into aBiggerArraymemcpy(aBiggerArray, anArray, sizeof(int)*5);


-me
That will only work if mainArray is a local/member variable or if you pass mainArray by reference.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Which language are you using? It isn't Java and it isn't C++...
It looks to me like he's trying to implement his own dynamic, resizing array type in C++.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Quote:Original post by Palidine
incedentally a better way of copying from array to array is using the memcpy method; it is infinitely faster than looping through the array and copying element by element


And does the wrong thing for non-POD types. It is therefore a bad idea.

Quote:Original post by gbizzle
Rect tempArray[] = new Rect[2 * mainArray.length];



What language are you using? You can't assign to a C (or C++) array. And they don't have a length attribute either.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Wouldn't this imply that mainArray is a Rect *? Where is this length variable coming from? Or is this not C++? And if not, what am I looking at?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
so sorry everyone i always forget, it's in Java. The Rect data type is a class that I've made elsewhere.

This topic is closed to new replies.

Advertisement