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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With