Depth Analysis of NULL pointer

Source: Internet
Author: User
  1. What is a null pointer constant (NULL pointer constant )?

    [6.3.2.3-3] an integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.

    Here we will tell you: 0, 0l, '/0', 3-3, 0*17 (they are all "integer constant expression") and (void *) 0 and so on are all NULL pointer constants (note (char *) 0 is not a null pointer constant, but a null pointer value ). The system selects which form is used as a null pointer constant. Generally, most of the C system choices (void *) are 0 or 0 (and some others choose 0l). As for C ++ systems, due to strict type conversion requirements, void * cannot be freely converted to other pointer types as in C. Therefore, 0 is usually selected as a null pointer constant, rather than void * 0.

  2. What is a null pointer )?

    [6.3.2.3-3] If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare ququal to a pointer to any object or function.

    Therefore, if p is a pointer variable, P = 0;, P = 0l;, P = '/0';, P = 3-3;, P = 0*17; after any value assignment operation (for C, it can also be P = (void *) 0;), p becomes a null pointer, the system ensures that the NULL pointer does not point to any actual object or function. Conversely, the address of any object or function cannot be a null pointer.

  3. What is null?

    [6.3.2.3-footnote] The macro null is defined in <stddef. h> (and other headers) as a null pointer constant

    Null is a standard macro definition used to represent NULL pointer constants. Therefore, in addition to the above assignment methods, you can also use P = NULL; To make p a null pointer.

  4. Where does a null pointer point to the memory (internal implementation of a null pointer )?

    The standard does not specify the location in the memory where the Null Pointer Points to, that is, the specific address value used (0x0 address or a specific address) indicates that the NULL pointer depends on the system implementation. Our common null pointers generally point to the 0 address, that is, the NULL pointer is represented by all 0 internally (zero NULL pointer, zero NULL pointer ); some systems also use special address values or special methods to express null pointers (nonzero NULL pointer, non-zero NULL pointer). For details, see the c faq.

    Fortunately, in actual programming, we don't need to know whether the pointer over our system is a zero NULL pointer or nonzero NULL pointer, we only need to know whether a pointer is a null pointer. the compiler will automatically implement the conversion and shield us from the implementation details. Note: Do not equate the internal representation of the NULL pointer with the object representation of the integer 0 -- as described above, sometimes they are different.

  5. How can I determine whether a pointer is a null pointer?

    This can be achieved by comparing it with a null pointer constant or other null pointers (note that it is irrelevant to the internal representation of the NULL pointer ). For example, if p is a pointer variable and Q is a null pointer of the same type, check whether P is a null pointer, either of the following forms can be used-they are equivalent in terms of implementation functions, and the difference is only the difference in style.

    Pointer Variable P is null pointer judgment:
    If (P = 0)
    If (P = '/0 ')
    If (P = 3-3)
    If (P = NULL)/* use null must contain the header file of the corresponding standard library */
    If (null = P)
    If (! P)
    If (P = q)
    ...

    Pointer Variable P is not a null pointer:
    If (P! = 0)
    If (P! = '/0 ')
    If (P! = 3-3)
    If (P! = NULL)/* null must contain the header file of the corresponding standard library */
    If (null! = P)
    If (P)
    If (P! = Q)
    ...

  6. Can I use the memset function to get a null pointer?

    This problem is equivalent to: If P is a pointer variable, then

    Memset (& P, 0, sizeof (p); and P = 0;

    Is it equivalent?

    The answer is no. Although it is equivalent in most systems, some systems have non-zero NULL pointer, which is not equivalent. For this reason, you should note that memset should not be used when you want to set the pointer to a null pointer. Instead, you should use a null pointer constant or a null pointer to assign values to or initialize the pointer variable.

  7. Can I define my own implementation of null? Q: Can the null value be equivalent to 1, 2, or 3? "Similar problems

    [7.1.3-2] if the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

    Null is a reserved identifier (retained identifier) that meets the preceding conditions in the standard library ). Therefore, if null is introduced with the corresponding standard header file, it is invalid to redefine null as different content in the program, and its behavior is undefined. That is to say, if it is a standard program, its null value can only be 0, not other values except 0, such as 1, 2, 3.

  8. Does the malloc function return 0 or null when memory allocation fails?

    The malloc function is a library function specified by standard C. The standard explicitly specifies that a "null pointer" (NULL pointer) is returned when the memory allocation fails ):

    [7.20.3-1] If the space cannot be allocated, a null pointer is returned.

    For NULL pointer values, the Common Document (such as man) tends to use null instead of 0. But we should be clear: For pointer types, returning null is equivalent to returning 0, because both null and 0 indicate "Null Pointer" (NULL pointer ).

References:

Null pointers (http://www.c-faq.com/null/index.html)

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.