{"id":98175,"date":"2018-03-07T07:00:00","date_gmt":"2018-03-07T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=98175"},"modified":"2019-03-13T00:49:48","modified_gmt":"2019-03-13T07:49:48","slug":"20180307-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180307-00\/?p=98175","title":{"rendered":"When MSDN says <CODE>NULL<\/CODE>, is it okay to use <CODE>nullptr<\/CODE>?"},"content":{"rendered":"<p>In various places, MSDN will talk about the behavior corresponding to the case where a handle type has the value <code>NULL<\/code>. A customer wanted to know whether it was safe to use <code>nullptr<\/code> in such cases, or whether they have to use <code>NULL<\/code>. <\/p>\n<p>Although the programming languages used by MSDN for documenting Windows are putatively C and C++, MSDN understands that a lot of people write code for Windows in other languages, and therefore it tries to avoid <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20050628-07\/?p=35183\">relying on language subtleties<\/a>. <\/p>\n<p>Esoteric definitions for the term <code>NULL<\/code> is one of those language subtleties. <\/p>\n<p>Formally, the C and C++ languages permit the following definitions for the <code>NULL<\/code> macro: <\/p>\n<table BORDER=\"1\" CLASS=\"cp3\" CELLPADDING=\"3\" STYLE=\"border-collapse: collapse\">\n<tr>\n<th><code>NULL<\/code><\/th>\n<th><code>0<\/code><\/th>\n<th><code>(void*)0<\/code><\/th>\n<th><code>nullptr<\/code><\/th>\n<\/tr>\n<tr>\n<th>C<\/th>\n<td>allowed<\/td>\n<td>allowed<\/td>\n<td>not allowed&sup1;<\/td>\n<\/tr>\n<tr>\n<th>C++<\/th>\n<td>allowed<\/td>\n<td>not allowed&sup2;<\/td>\n<td>allowed<\/td>\n<\/tr>\n<\/table>\n<p>If <code>NULL<\/code> is defined as <code>(void*)0<\/code> in C or as <code>nullptr<\/code> in C++, then it can be assigned only to a pointer type. And since MSDN cannot control how the C and C++ header files define <code>NULL<\/code>, it needs to work with any definition that is permitted by the corresponding standards. Which means that saying <code>NULL<\/code> implies that the underlying type is a pointer type. <\/p>\n<p>Therefore, you are welcome to write <code>nullptr<\/code> instead of <code>NULL<\/code> if you&#8217;re writing C++ code. You&#8217;re also welcome to write anything else that produces a null pointer, such as <\/p>\n<pre>\nHMUMBLE h1 = HMUMBLE();\nHMUMBLE h2 = HMUMBLE{};\nHMUMBLE h3 = HMUMBLE(0);\nHMUMBLE h4 = (HMUMBLE)0;\nHMUMBLE h5 = 3 - 3;\n<\/pre>\n<p>But most people would probably prefer you to write <code>NULL<\/code> or <code>nullptr<\/code>. <\/p>\n<p>As noted, MSDN understands that a significant portion of its readership is not fluent in the subtleties of C and C++. When it writes <code>NULL<\/code>, it means the obvious thing: A null pointer. You can translate that into the appropriate construction for the language you are using. For example, for C#, you can use <code>null<\/code>, or if you are operating in raw <code>IntPtr<\/code>s, you can use <code>IntPtr.Zero<\/code>. <\/p>\n<p><b>Bonus chatter<\/b>: When MSDN says <code>NULL<\/code>, is it okay to use <code>0<\/code>? Yes, but you probably don&#8217;t want to. Using <code>0<\/code> as a null pointer constant is permitted by the C and C++ languages for backward compatbility reasons, but it&#8217;s not considered modern style. <\/p>\n<p><b>Bonus bonus chatter<\/b>: I&#8217;m told that the Visual C++ folks occasionally entertain the possibility of changing the definition of <code>NULL<\/code> to <code>nullptr<\/code>, which is permitted by the standard. However, this ends up breaking a lot of code which assumed that <code>NULL<\/code> is an integral constant evaluating to zero. For example: <\/p>\n<pre>\nvoid foo(char* p)\n{\n  char c = NULL; \/\/ would not work if NULL were defined as nullptr\n  *p = NULL;     \/\/ would not work if NULL were defined as nullptr\n  ...\n}\n<\/pre>\n<p>Although that code is technically already broken, it manages to work if <code>NULL<\/code> is defined as <code>0<\/code>, and updating the definition in the language header files would break existing (albeit poorly-written) code. <\/p>\n<p>&sup1; C does not have the <code>nullptr<\/code> keyword. <\/p>\n<p>&sup2; C++ does not allow <code>NULL<\/code> to be defined as <code>(void*)0<\/code> because C++ does not permit implicit conversion from <code>void*<\/code> to arbitrary <code>T*<\/code>. <\/p>\n<pre>\nint* p = (void*)0; \/\/ allowed in C but not C++\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One of many equivalent formulations.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-98175","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>One of many equivalent formulations.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=98175"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98175\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=98175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=98175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=98175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}