increasing array size
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
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.
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.
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:
-me
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.
It looks to me like he's trying to implement his own dynamic, resizing array type in C++.
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 gbizzleRect 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.
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement