Explanation of C language hollow pointer, NULL pointer constant, and NULL 0

Source: Internet
Author: User
Tags define null

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 (tyc: I think (void *) 0 should be regarded as a null pointer. It is more appropriate.) It is always a null pointer constant (note (char *). 0 is not a null pointer constant, only 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 (tyc: C ++ standard recommendation) instead of void *) 0.

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. (Tyc: for example, here (void *) 0 is a null pointer. Understanding it as null pointer or null pointer constant will be different in microseconds, of course it doesn't matter)

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. (Tyc: implementation in many systems: # define NULL (void *) 0, which is not exactly the same as "a null pointer constant" here)

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.

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) <--------------- (here and below if (p) have been specifically said once, the definition of null is not necessarily 0. if it is used rashly here! P is very dangerous, so this writing cannot be promoted)
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)
...
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.
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.

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 ). (Tyc: Generally, NULL is returned in the manual in the system. Let's use NULL)

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.