Delete NULL but no compile error

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

    It is a de-facto standard in C and C++ languages (and not only in them) that resource deallocation routines must accept null-pointer arguments and simply do nothing. Actually, it is a rather convenent convention. So, the real question here: why does it surprize you? What makes you think that it should produce an error? Moreover, what makes you think that it should fail to compile???

    BTW, your question, the way it is stated, doesn't seem to make much sense, since your code actually cannot compile. The supposed pointer declaration lacks a type, which will make any compiler to issue a diagnostic message.

提交回复
热议问题