I have read here, as well as elsewhere, that deleting the same variable twice can be disastrous (even when there is more than one variable name).
Suppose I have a function which takes an input and output array:
void test(int*& input, int*& output) {
if(input[0] == 0) {
output = input;
}
}
and it may assign a pointer to another variable I'm using:
int *input = new int[3];
int *output = new int[3];
input[0] = 0;
test(input, output);
delete[] input;
delete[] output;
how can I avoid the double delete?
In this overly simplified scenario, I know I could check the pointer addresses to see if they are equal and conditionally only delete one of them, but is there a better solution when I won't know pointers might be pointing to the same memory?
Edit:
tidied up the things to avoid some confusion..