Jump to content
  • Advertisement
Sign in to follow this  
gbizzle

increasing array size

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

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 aBiggerArray
memcpy(aBiggerArray, anArray, sizeof(int)*5);



-me

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!