I read here , as well as elsewhere, that deleting the same variable twice can be catastrophic (even if there is more than one variable name).
Suppose I have a function that accepts an input and output array:
void test(int*& input, int*& output) { if(input[0] == 0) { output = input; } }
and it can assign a pointer to another variable that I use:
int *input = new int[3]; int *output = new int[3]; input[0] = 0; test(input, output); delete[] input; delete[] output;
How can I avoid double deletion?
In this oversimplified scenario, I know that I can check the addresses of pointers to make sure they are equal and conditionally delete only one of them, but is there a better solution when I don’t know that pointers can point to the same memory?
Edit:
tidied up everything to avoid confusion.
source share