delete a NULL pointer is safe

来源:互联网 发布:http aq.js cj.com 编辑:程序博客网 时间:2024/05/17 02:03

C programmer often writes codes in the following fashion:

 

if(p){

    free(p);

    p = NULL;

}

 

But in C++ scenario, the check of NULL is useless!

 

C++ guarantees that operator delete checks its argument for null-ness. If the argument is 0, the delete expression has no effect. In other words, deleting a null pointer is a safe (yet useless) operation. There is no need to check the pointer for null-ness before passing it to delete:

 

if(p) // useless; delete already checks for a null value

  delete(p);


转载http://blog.csdn.net/psusong/article/details/5048791

原创粉丝点击