Delete NULL but no compile error

后端 未结 5 1717
悲哀的现实
悲哀的现实 2021-01-18 06:12

I\'m confused why the following C++ code can compile. Why does a call to delete the method of 0 not produce any error?!

int *arr = NULL;     // or if I use 0         


        
5条回答
  •  一向
    一向 (楼主)
    2021-01-18 06:37

    NULL and 0 aren't the same thing. In C++ you should use 0.

    There is nothing syntactically wrong or ambiguous about deleting the null pointer. In fact, this is by definition a no-op; that is, the operation of deleting the 0th address is equivalent to doing nothing at all.

提交回复
热议问题