Delete NULL but no compile error

后端 未结 5 1719
悲哀的现实
悲哀的现实 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:38

    You can delete a NULL pointer without problem, and the error you may/can have won't be at compilation time but at runtime.

    int *ptr_A = &a;
    ptr_A = NULL;
    delete ptr_A;
    

    Usually it's convenient to do :

    ...
    delete ptr;
    ptr = NULL;
    

提交回复
热议问题