Talk:Delete (C++)

Latest comment: 11 years ago by 71.65.94.23 in topic NULL vs. 0

using the wrong delete is a vulnerability edit

The last paragraph is too hairy, I don't want too touch it, but it should explain that using the wrong delete is a security vulnerability, linking to: http://replay.web.archive.org/20080703153358/http://taossa.com/index.php/2007/01/03/attacking-delete-and-delete-in-c —Preceding unsigned comment added by 99.224.97.6 (talk) 18:00, 14 May 2011 (UTC)Reply

NULL vs. 0 edit

I recently made an edit to initialize pointers in the code snippets with NULL instead of 0. The "edit summary" field was insufficient for explaining my justification for doing so and several previous edits changed NULL to 0 with the following vague and nebulous justifications:

"int *p_var = NULL" is still valid C++ syntax, but is very C-style, making it equal zero is more C++

Changed NULL to 0, although null is same as 0, it should be avoided.

Frankly, neither of these are good reasons. NULL is no less "C++ style" (whatever that is) than int, the ++ operator, or for. And there is no indication as to why NULL should be avoided. The only reason to avoid using NULL is, as Bjarne Stroustrup explains, to avoid macros whenever possible (http://www.stroustrup.com/bs_faq2.html#null). However, this rational is purely a matter of personal preference and NULL is part of the C++ standard. Using NULL instead of 0 produces more self-documenting code (i.e., when manipulating a pointer value, setting it to NULL indicates to a human reader that the variable is a pointer without needing to reference the declaration). This is driven home by the fact that C++11 defines nullptr (of course, if you're writing to the C++11 standard, nullptr would be much preferred to NULL). Also, if you're using the GCC C++ compiler (I don't know about other compilers) NULL is #define NULL __null which means the compiler will always understand the rvalue to be a pointer value (http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt02ch04s03.html). 71.65.94.23 (talk) 03:45, 26 October 2012 (UTC)Reply