Why, really, deleting an incomplete type is undefined behaviour?

To combine several answers and add my own, without a class definition the calling code doesn’t know: whether the class has a declared destructor, or if the default destructor is to be used, and if so whether the default destructor is trivial, whether the destructor is accessible to the calling code, what base classes exist … Read more

When to use new and delete

Rather then telling you when to use delete, I’ll try to explain why you use pointers anyway. So you can decide when to use dynamic objects, how to use them and so when to call delete (and not). Rules of thumb: Use static objects where possible, then when needed create a pointer to that instance. … Read more

Deleting a pointer in C++

1 & 2 myVar = 8; //not dynamically allocated. Can’t call delete on it. myPointer = new int; //dynamically allocated, can call delete on it. The first variable was allocated on the stack. You can call delete only on memory you allocated dynamically (on the heap) using the new operator. 3. myPointer = NULL; delete … Read more