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

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

    delete e;
    delete e;
    ...
    delete e;
    return 0;
}
7条回答
  •  时光说笑
    2020-11-28 10:06

    If you are worried this might happen in your apps, either stop using raw pointers completely, so that you don't need delete (eg switch over to shared_ptr) or always set pointers to NULL (or 0, or better still nullptr) after you delete them. Calling delete on a null pointer is guaranteed to do nothing.

提交回复
热议问题