Copying one pointer to another element by element in C++ and getting "Error in `./hsc.exe': double free or corruption (!prev): 0x0000000000aadcc0 *** Aborted (core dumped)".
I tried to debug it and I get stuck at the line "delete[] rods;" as I can't delete rods as I am also deleting the information related to "temp".
Here is the following part of the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int * rods; // Defining rods and temp
int * temp;
int N_r =5;
rods = newint[N_r];
temp = newint[N_r];
for (int i = 0; i < N_r; i++){ // Copying rods to temp
temp[i] = rods[i];
}
delete[] rods; // deleting rods
rods = NULL;
rods = newint[N_r]; // creating new rods
for (int i = 0; i < N_r; i++){ // copying temp to rods
rods[i] = temp[i];
}
delete[] temp; // delete temp
temp = NULL;[code]
In debugger, I get problem at line 10 as this information is also being used by another pointer "temp". I can't delete[] rods at the end of it as I have to use rods[] for other purposes. Maybe I didn't understand what you said. :)