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:
#include <iostream>
int main(){
int max_size= 5;
int* parray = newint[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 = newint[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:
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!