当你在 C++ 中释放一个指针两次或更多次时会发生什么? [英] What happens when you deallocate a pointer twice or more in C++?

查看:25
本文介绍了当你在 C++ 中释放一个指针两次或更多次时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main() {
    Employee *e = new Employee();

    delete e;
    delete e;
    ...
    delete e;
    return 0;
}

推荐答案

如果您多次尝试通过指针删除一个对象,您会得到未定义行为.

You get undefined behaviour if you try to delete an object through a pointer more than once.

这意味着几乎任何事情都可能发生,从似乎可以工作"到崩溃"或完全随机的事情.

This means that pretty much anything can happen from 'appearing to work' to 'crashing' or something completely random.

这篇关于当你在 C++ 中释放一个指针两次或更多次时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆