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

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

    delete e;
    delete e;
    ...
    delete e;
    return 0;
}
7条回答
  •  我在风中等你
    2020-11-28 10:02

    You are likely venturing into 'undefined behavior' territory.

    On many systems this will cause a crash; for example, on my Linux machine:

    *** glibc detected *** ./cctest: double free or corruption (fasttop): 0x0000000000d59900 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x7f399f4cbdd6]
    /lib/libc.so.6(cfree+0x6c)[0x7f399f4d074c]
    ./cctest[0x400a7a]
    /lib/libc.so.6(__libc_start_main+0xfd)[0x7f399f474abd]
    ./cctest[0x400959]
    

提交回复
热议问题