<分区>
Possible Duplicate:
Is there any reason to check for a NULL pointer before deleting?
我经常在代码中看到如下内容:
if(pointer)
delete pointer;
据我了解,删除空指针是安全的,那么此检查的意义何在?
<分区>
Possible Duplicate:
Is there any reason to check for a NULL pointer before deleting?
我经常在代码中看到如下内容:
if(pointer)
delete pointer;
据我了解,删除空指针是安全的,那么此检查的意义何在?
最佳答案
delete
将为您检查指针是否为 NULL,因此您是对的,不需要检查。
您可能还会看到有些人将指针设置为 NULL after 它被删除,这样您就不会做任何愚蠢的事情,比如尝试使用不再属于您的内存或阻止您删除指针两次,这将导致错误。
关于c++ - 删除空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8004495/