c++pointersmemory-leaksdelete-operatordouble-free

What happens in a double delete?


Obj *op = new Obj;
Obj *op2 = op;
delete op;
delete op2; // What happens here?

What's the worst that can happen when you accidentally double delete? Does it matter? Is the compiler going to throw an error?


Solution

  • It causes undefined behaviour. Anything can happen. In practice, a runtime crash is probably what I'd expect.