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.
int * rods; // Defining rods and temp
int * temp;
int N_r =5;
rods = new int[N_r];
temp = new int[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 = new int[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;