I wrote this program to create more memory for arrays as soon as i input more than 10 numbers. When i use the function in line 15 i return the array numbers but i don't return the new size (and i think that's why i have some garbage numbers when i cout), i increment it on the next line by 10 but i think that's where the problem is.
For example i input 13 times "1" i have a cout loop of thirteen "1"s, 0,1995519536,0,0,0,1995519536,0. I should have only "0"s after 13 "1"s. This doesn't happen if there are only 10 numbers in the array, when the "if" of line 14 doesn't apply, so i think the problem is there.
You will see junk values if you enter less than 10 numbers as well.
size is the capacity of the array, while next is the amount of non-junk values in it.
Typically, we use "size" or "length" to describe the amount of values in a container, and "capacity" to describe the amount of values that can be stored without a re-allocation.
LowestOne, I agree with you on line 17, but if you don't initialize the values when you dynamically allocate memory then C++ will give you junk values. That's is why you get junk values because they are not initialized.
Why should they be initialized? The OP wrote: "when I input".
Therefore, we have an array and we assign values to it. When we want to assign more values than the array can hold, we must increase the capacity of the array. We will have at least one value to put into the new space, but why should we suddenly have 20 integers, if we have got only 11 from input stream?
The std::vector does exactly the same thing, when we push values to it; it does not initialize allocated but unused memory.
There is a problem on line 23: you print size elements, but you only have next elements.
@rafae11 The std::vector is a good source of inspiration and a good tool for "real work", but if the task is to learn to think, then vector can not do it for us.
Why should they be initialized? ... When we want to assign more values than the array can hold, we must increase the capacity of the array.
Yeah when you increase the size of the array you don't set what values are of the new allocated space.When you allocate new memory an you don't initialize the value you get junk values. Take the following example.
why should we suddenly have 20 integers, if we have got only 11 from input stream?
The program does not include the feature to figure out the amount of space needed depending on the input, so he just increments by 10 to make it simpler.