Are you allowed to copy a pointer and call delete on the copy?

If I copy a pointer and then ask for the pointed to allocation to be deleted, is it valid C++, or is it undefined behavior?

char* orig = new char[100];
char* copy = orig;
delete[] copy;


  • It's perfectly valid. You are deallocating the array that you allocated on the first line.

    Note that copying the pointer doesn't copy the allocated array. So you allocate one array and then you deallocate one array, leaving you with no arrays remaining.