What happens when you deallocate a pointer twice or more in C++?

前端 未结 7 2306
自闭症患者
自闭症患者 2020-11-28 09:12
int main() {
    Employee *e = new Employee();

    delete e;
    delete e;
    ...
    delete e;
    return 0;
}
7条回答
  •  死守一世寂寞
    2020-11-28 09:57

    If you're really lucky it will crash. What normally happens is it stores up karma until your CEO is demonstrating the code to your most important new customer when it will corrupt/destroy all of their data.

    In checked or debug builds often this kind of thing is caught, but it can go completely undetected and cause havoc later. This is especially profound when multiple threads get involved.

提交回复
热议问题