0

Title says it all more or less. When I need an (for the sake of this example) integer array for an unknown amount of values I know I can change it's size using new *array = new int[size]. Now my question is: If I have an array of a certain size, but I need to make it bigger, can I just use the new operator to expand it and will it still have all previously stored elements or would it be smarter to create a whole new array with a dynamic size, copy all elements from the previous array into the new one and delete[] the old array. Basically just swapping between the two arrays, whenever I need a new size.

Specifically I am asking whether or not this piece of code would work in the way it's intended to work

for(int i = 1; i < 10; i++){
int *array = new int[i];
array[i-1] = i;
}

My assumption is that this array will first be the size of 1 and store the value 1 at index 0. Then it will reallocate its size to 2 and store the value to at index 1 and so on until i is 9.

I guess to rephrase my question a bit better: Does an array initialized with new have to be populated with elements or will it copy the elements it had from before using the operator?

Ralph
  • 70
  • 2
  • 11
  • 2
    `new *array = new int[i];` looks strange. A not compiling code isn´t good to explain what you mean. – deviantfan Nov 22 '14 at 11:06
  • Please post some realistic code. – juanchopanza Nov 22 '14 at 11:08
  • And no. Allocate a second bigger array, copy the content, and delete[] the old one. Yes, with large arrays this can get slow. If you need something more flexible, there are other data structures like lists, trees ... with their own advantages and disadvantages compared to an array. – deviantfan Nov 22 '14 at 11:08
  • @deviantfan fixed was a typo sorry about that – Ralph Nov 22 '14 at 11:16

3 Answers3

2

You can't resize the array in this way. You need to make a new array and then copy the old array into it. You can also try std::vector, which does what you want automatically.

If you want to use pointers rather than std::vector to change the size of your array, you can do it in this way.

int n = 100; // This will be the number of elements.

int *array1; // Pointer

array1 = new int[n]; // This will allocate your array with size n, so you will have 100 elements. You can combine this with the previous in int *array1 = new int[n];

So fill up the this array however you please... Then you decide you want a 200 element array instead? You will need to create a different array in the same way.

int *array2 = new int[200]; 

You can use the for loop to copy array 1 into array 2. The for loop should iterate as many times as there are elements in array 1 (100).

for(int i = 0; i < 100; ++i)
    array2[i] = array[1];

At this stage array2 is exactly the same as array1, but with 100 uninitialized elements at your disposal from [100] to [199].

You won't need array1 anymore, so at some point, you should call

delete [] array1;

Your assumption, by the way would not work, because on the first cycle of your loop, you create (or try to create) an array of i=1 element. Arrays start counting at 0, so your only single element is [0]. When i is at 0, what is i-1?

If you try to access array[-1], you'll probably crash. But why should you want to create 10 different arrays? new keyword creates an unrelated object, not overwrites the one with the same name.

Itolet
  • 189
  • 6
1

Does an array initialized with new have to be populated with elements or will it copy the elements it had from before using the operator?

new[] allocates new array, completely independent from previous.

I know 3 ways to "make the array bigger":

  1. As @ravi mentioned, don't mess with poinsters, use modern std::vector.
  2. Make new array in new pointer, std::move elements from old array to the new one, and then delete[] old array.
  3. Get rid of new[] & delete[], use old realloc with malloc & free.
Community
  • 1
  • 1
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
0

You have to allocate new array and copy old array's data into that. This is how vector is implemented. Had there been better way of doing it, C++ standard community would have considered that.

ravi
  • 10,994
  • 1
  • 18
  • 36
  • @Puppy In this case yes as containers are the backbones of STL. Any possible optimization in these could prove to be a huge gain. – ravi Nov 22 '14 at 11:33