c++ Dynamically allocating arrays through functions

Hi, so I learned a basic gist of dynamically allocating information, namely in the form of arrays. I first learned by creating a temporary pointer to copy information from the original array, and then point that original to the new one. Int the following code, i essentially ask the user to input numbers, and when the max_size has been reached, then the program will automatically create a bigger array. And this worked, as shown below:

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
37
38
39
40
41
42
43
44
45
46
  #include <iostream>

int main(){
    int max_size= 5;
    int* parray = new int[max_size];
    int* temp = 0;

    int index = 0;
    int input = 1;


    while(input!=0){




        if(index==max_size){
            int new_size = max_size +5;
            temp = new int[new_size];
            for(int i = 0; i<max_size;i++){
                temp[i]=parray[i];
            }
            delete [] parray;
            parray= temp;
            temp = 0;
            max_size = new_size;

        }


        std::cout<<"input number\n";
        std::cin>>input;

        parray[index] = input;
        index++;

    }

    for(int i = 0; i<index;i++){
        std::cout<<parray[i]<<" ";
    }

    delete [] parray;
    return 0;
}



this works as expected. However, i wanted to do the resizing of the array in a separate method. When i do this, at some point in the code, the values turn to garbage. I have not been able to figure this out for about two weeks, so if you could help me and tell me what is wrong and why, i would be very thankful! here is the code:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

void resize(int*pint, int &size){
    int new_size = size +5;
    int* temp = new int[new_size];
    for(int i = 0; i<size;i++){
        temp[i]=pint[i];
    }
    delete [] pint;
    pint= temp;
    temp = 0;
    size = new_size;
}


int main(){
    int max_size= 5;
    int* parray = new int[max_size];

    int index = 0;
    int input = 1;


    while(input!=0){




        if(index==max_size){
            resize(parray, max_size);

        }


        std::cout<<"input number\n";
        std::cin>>input;

        parray[index] = input;
        index++;

    }

    for(int i = 0; i<index;i++){
        std::cout<<parray[i]<<" ";
    }

    delete [] parray;
    return 0;
}


thanks again!
Last edited on
you can either return the new pointer or use a reference (& -> int*&pint) to the pointer as you did for int &size
Thanks! I revisited a section on passing by address, and it said that the addresses are still passed by value, thus if the address itself changes in the function, then the original copy will not change. I simply thought that by passing pointers, you will be able to change everything. Thank you for the clear-up!
Topic archived. No new replies allowed.