resize array using pointers

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

int *growarray(int* values,int cursize);

int main (){
int next=0;
int size=10;
int* values=new int[size];
int val;
cout << "enter num\n";
cin >> val;
while(val>0){
    if(size==next){
        values=growarray(values,size);
        size+=10;
    }
    values[next]=val;
    cout << "enter 0 to exit or another num ";
    cin >> val;
    next++;
}
for(int i=0;i<size;i++){
    cout << values[i] << endl;
}
}

int *growarray(int* values,int cursize){
int newsize=cursize+10;
int *newvalues=new int[newsize];
for(int i=0;i<cursize;i++){
    newvalues[i]=values[i];
}
delete values;
return newvalues;
}
do you have to use arrays.
if you don't have a read on vectors.

http://www.cplusplus.com/reference/vector/vector/
What do you mean by junk vals? Can you give us a sample output?
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.

Also, to delete an array:
delete [] my_array;
Last edited on
Looks like when you allocate newvalues they contain junk values. try initializing the values of newvalues like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int *growarray( int* values, int cursize ){

  int newsize = cursize+10;
  int *newvalues = new int[newsize];

  //Initilize values
  for( int i = 0; i < cursize; i++ )
       newvalues[i] = 0;

  for( int i = 0; i < cursize; i++ )
       newvalues[i] = values[i];
  
  delete[] values; //deallocate arrays with delete[]
  return newvalues;

  delete[] newvalues; //Don't forget newvalues too.

}

Hertz, your code never reaches line 17, nor should you delete newvalues within the function anyway. There is also no reason to zero the memory.
Last edited on
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.
Last edited on
@keskiverto
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
main() {
    
    //Uninitialized array
    int *uninit = new int[10]; //Uninitialized array
    
    int *init = new int[10]; //Initliazed array
    for( int a = 0; a < 10; a++ )  //Set all values to 0.
        *( init + a ) = 0;
        
    cout << "Not initialized : ";
    for( int i = 0; i < 10; i++ ) 
        cout << *( uninit + i ) << " ";
    
    cout << "\nInitialized : ";
    for( int i = 0; i < 10; i++ ) 
        cout << *( init + i ) << " ";      
    
    delete[] uninit;
    delete[] init;
    
}


This is my output :

Not initialized : 3746456 3740968 1698978932 1550284130 1198418253 1767320919 2003788910 2776179 0 0 
Initialized : 0 0 0 0 0 0 0 0 0 0 



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.
We understand what it is to be initialized.

The point is that there is no reason to zero the data because a value of zero is just as much junk as a random number.

Anyway, OP, like keskiverto and I have said, your loop at line 23 should iterate until i reaches next.
Topic archived. No new replies allowed.