Difference between NULL and 0

1. What is a 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.

This tells us: 0, 0L, '\0', 3 - 3, 0 * 17(they are all "integer constant expressions"), and (void*)0etc. are all null pointer constants (note that (char*)0is not called a null pointer constant, just a null pointer value).

As for which form the system chooses to use as a null pointer constant, it is implementation-dependent. The general C system chooses (void*)0or 0is mostly (there are also individual choices 0L);

As for the C++ system, due to strict type conversion requirements, it void*cannot be freely converted to other pointer types as in C, so it is usually selected 0as a null pointer constant (tyc: recommended by the C++ standard), not selected (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 unequal to a pointer to any object or function.

Therefore, if it pis a pointer variable, after any of the assignment operations in p = 0;, p = 0L;, p = '\0';, p = 3 - 3;, , p becomes a null pointer, == is guaranteed by the system 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 is a null pointer. Understanding it as a microsecond difference, of course, does not matter)p = 0 * 17;p = (void*)0;(void*)0null pointernull pointer constant

==wcy: That is to say, after assigning a null pointer constant to a pointer, the pointer is a null pointer?==

3. What is NULL?

[6.3.2.3-Footnote] The macro NULL is defined in

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

The standard does not specify where in memory the null pointer points, that is to say, which specific address value ( 0x0address or a specific address) is used to represent the null pointer depends on the implementation of the system. Our common null pointers generally point to the 0 address, that is, the interior of the null pointer is represented by all 0s (zero null pointer, zero null pointer); some systems use some special address values ​​or special methods to represent null pointers (nonzero null pointer) , a non-zero null pointer), see the C FAQ for details.

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

5. How to judge whether a pointer is a null pointer?

This can be done by comparing with null pointer constants or other null pointers (note that it has nothing to do with the internal representation of null pointers). For example, assuming p is a pointer variable and q is a null pointer of the same type, to check whether p is a null pointer can take any of the following forms - they are all functionally equivalent, the difference being It's just a difference in style.

The pointer variable p is a null pointer judgment:

if ( p == 0 )
if ( p == '\0' )
if ( p == 3 - 3 )
if ( p == NULL )  /* 使用 NULL 必须包含相应的标准库的头文件 */
if ( NULL == p )
if ( !p )
if ( p == q )
...

The pointer variable p is not a null pointer judgment:

if ( p != 0 )
if ( p != '\0' )
if ( p != 3 - 3 )
if ( p != NULL )  /* 使用 NULL 必须包含相应的标准库的头文件 */
if ( NULL != p )
if ( p )
if ( p != q )
...

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

This question is equivalent to: if p is a pointer variable, are
memset( &p, 0, sizeof(p) );and p = 0;
are equivalent?

The answer is no, although it is equivalent on most systems, == is not equivalent because some systems have a "nonzero null pointer==".

For this reason, it should be noted that when you want to set a pointer to a null pointer, you should not use memset, but should use a null pointer constant or a null pointer to assign or initialize a pointer variable.

7. Can you define your own implementation of NULL? Also answer "Can the value of NULL be 1, 2, 3, etc.?" similar questions

[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 in the standard library that meets the above conditions. Therefore, if the corresponding standard header file is included and NULL is introduced, it is illegal to redefine NULL as a different content in the program, and its behavior is undefined. That is to say, if it is a program that conforms to the standard, the value of NULL can only be 0, and cannot be other values ​​other than 0, such as 1, 2, 3, and so on.

8. Does malloc function return 0 or NULL when allocating memory fails?

mallocFunctions are standard C-specified library functions. It is clearly stated in the standard that a "null pointer" (null pointer) is returned when its memory allocation fails:

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

For null pointer values, the general documentation (such as man) tends to use NULL to represent, rather than directly say 0. But we should be clear: for pointer types, returning NULL and returning 0 are exactly equivalent, because both NULL and 0 mean "null pointer" (null pointer). (tyc: NULL is returned in the manuals in general systems, so let's use NULL)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325440485&siteId=291194637