Is it safe to delete every pointer in C++ as a pointer to an array?
Will it be fine for me if I will always write delete [] ptr
with no regard what ptr
actually is - whether it is a pointer to a single object or an array of objects?
Is it safe to delete every pointer in C++ as a pointer to an array?
Will it be fine for me if I will always write delete [] ptr
with no regard what ptr
actually is - whether it is a pointer to a single object or an array of objects?
No, it won't be. Deleting something with delete []
that was not allocated with new []
produces undefined behaviour. And why would you want to? You allocated the memory, so you should know how to delete it. For further information, see http://en.cppreference.com/w/cpp/language/delete.