Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to delete every pointer in C++ as a pointer to an array? [duplicate]

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?

like image 970
ggghahaha Avatar asked Jan 25 '26 10:01

ggghahaha


1 Answers

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!