C + + notes: The difference between null and 0, the origin of nullptr

Source: Internet
Author: User
Tags define null

At some point, we need to assign the pointer to a null pointer to prevent the wild pointer. Some people prefer to use NULL as a null pointer constant, for example: int* p = NULL;. There is also a direct use of the 0 value as a null pointer constant, for example: int* p = 0;. The former may think: null as a null pointer constant, the name is very image, readability is strong. The latter may feel that NULL is not a keyword for C/+ + languages, but a macro defined in the standard library header file <stddef.h>, so to use NULL, you may need to include the <stddef.h> header file directly or briefly, which is more cumbersome. question one: What is the difference between a null and a constant 0 value? To figure this out, we use a question and answer form to describe it. Q: What exactly is null? Answer: null is a macro. Q: What is the value of it? A: the C + + standard stipulates that its value is a null pointer constant (null pointer constant), which is defined by the implementation. #1, #2 Q: What kind of values can be called NULL pointer constants? A: C language constants 0 and (void*) 0 are null pointer constants, C + + (ignoring c++11) constant 0 is, and (void*) 0 is not. #3, #4 Q: Where is the null macro defined? A: Usually in the C standard library <stddef.h> header file, but other header files may also be defined. Q: How is a null macro defined in the General compiler's <stddef.h> header file? A: In the case of GCC or clang compilers, the definition of NULL is roughly as follows (slightly simplified):
#if defined (__cplusplus)0    //  C + + uses 0 as a null value #else# Define NULL ( (void *)0)    //  C with a value of ((void *) 0) as null #endif 

Q: Why C (void*) 0 is a null pointer constant, but not in C + +? A: Because any type of pointer in the C language can (implicitly) be converted to the void* type and vice versa, the void* type in C + + cannot be implicitly converted to another type pointer (for example: Int*p = (void*) 0; compile with C + + compiler will error). #5, #6 Q: Since the C + + standard, constant 0 is available as a null pointer constant, why not use 0 uniformly? A: Personally feel that because (void*) 0 more can reflect the meaning of pointers, and the constant 0 more time is used as an integer. Therefore, the null definition in the C language is selected (void*) 0. (for reference only) question two: Why do you want to introduce nullptr in c++11? Consider the case of such a function overload:
#include <stddef.h>void foo (int) {}     //  #1void foo (Char *) {}   //  #2int  main () {    //  Call # # or # # #? }

Literally, NULL is a null pointer constant, and we might think that since it's a pointer, you should call # #. In fact, it is called # # because the null extension in C + + is constant 0, which is an int type. The root cause is: constant 0 is both an integer constant and a null pointer constant. To solve this ambiguity, the C++11 standard introduces the keyword nullptr, which acts as a null pointer constant. #7例如:
void foo (int) {}     //  #1void foo (char*) {}   // #2 int Main () {    //  It will call # # without any objection}

notes: [#1] c99:7.17-p3:    The macros are        null    which expands to an impl ementation-defined null pointer constant; And ...  [#2] c++03:18.1-p4:    The macro NULL is a implementation-defined C + + NULL pointer constant in This International standard (4.10).  [#3] c99:6.3.2.3-p3:    A integer constant expression with the value 0 , or such an expression cast to type void *, is called a null pointer constant. [#4] c++03:4.10-p1:    a n Ull pointer constant is a integral constant expression (5.19) Rvalue of integer type that evaluates to zero. [#5] C9 9:6.3.2.3-p1:    A pointer to void is converted to or from A pointer to any incomplete or object type. A pointer to any incomplete or object type is converted to a pointer to void and back again; The result shall compare equal to the original pointer. [#6] c++03:4.10-p2:    An rvalue of type "pointer To CV T, "WHere's a object type, can be converted to a rvalue of type "pointer to CV void."  [#7] c++11:4.10-p1:    A null pointer constant is an integral constant expression (5.19) Prvalue of Integ ER type, evaluates to zero or a prvalue of type std::nullptr_t.  Reference:(1) C99/C++03/C++11 standard document (2) nullptr proposal Document: N2431:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf

C + + notes: The difference between null and 0, the origin of nullptr

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.