在双删除中会发生什么?

在双删除中会发生什么?

问题描述:

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?

它导致未定义的行为。任何事情都可能发生。实际上,运行时崩溃可能是我期望的。

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