Parsing C-language hollow pointers, null pointer constants, NULL & 0, _c language

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 a expression cast to type void *, is called a null p OINTER constant.

This tells us: 0, 0L, ' yes ', 3-3, 0 * 17 (They are both "integer constant Expression") and (void*) 0 (TYC: I think (void*) 0 should be a null pointer, More appropriately) are NULL pointer constants (note (char*) 0 is not called null pointer constant, just a null pointer value). As to which form the system chooses to use as null pointer constants, the implementation is relevant. General C System Selection (void*) 0 or 0 of the majority (also have individual choice of 0L); As for C + + systems, because of the strict type conversion requirements, void* can not be as free to convert to other pointer types as C, so usually choose 0 as null pointer constants (tyc:c++ standard recommended) , without selecting (void*) 0.

What is a null pointer (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 GU Aranteed to compare unequal to a pointer to any object or function.

So if p is a pointer variable, then p = 0;, p = 0L;, p = ' yes ';, p = 3-3;, p = 0 * 17; After any of the assignment operations (which can also be P = (void*) 0 for C), p becomes a null pointer, and the system guarantees 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.) To think of it as null pointer or null pointer constant can be microseconds different, of course, not critical.

What is NULL?

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

That is, NULL is a standard defined macro definition used to represent null pointer constants. Therefore, in addition to the above various assignment methods, you can also use P = NULL; To make p a null pointer. (TYC: Implementations in many systems: #define NULL (void*) 0, which is not exactly the same as "a null pointer constant" here)

where does the null pointer (null pointer) point to memory (internal implementation of the null pointer)?

The standard does not specify where the null pointer points to memory, that is, whether the specified address value (0x0 address or a specific address) indicates that the null pointer depends on the implementation of the system. Our common null pointers generally point to 0 addresses, that is, the interior of the null pointer is represented by a full 0来 (zero null pointer, 0 null pointers); some systems represent null pointers (nonzero null pointer, not 0 null pointers) in special address values or special ways. See the C FAQ for details.

Fortunately, in actual programming, it is not necessary to know whether the pointer over our system is a zero null pointer or a nonzero null pointer, we just need to know if a pointer is a null pointer--the compiler automatically implements the conversion, Mask the implementation details for us. Note: Do not represent an object that is equivalent to an integer 0 in the internal representation of an empty pointer--as described above, sometimes they are different.

How can I tell if a pointer is a null pointer?

This can be done by comparing the null pointer constants or other NULL pointers (note that is independent of 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, to check if p is a null pointer, you can take any of the following forms-they are equivalent in the functionality of the implementation and differ only in stylistic differences.

the pointer variable p is the null pointer's judgment:
if (p = = 0)
if (p = = ' ")
if (p = = 3-3)
if (p = = null)/* Use NULL must contain the corresponding standard library header file * *
if (NULL = p)
if (!p) <---------------(here and the following if (p) are specifically said once, the null definition is not necessarily 0. It would be dangerous to use!p here. So this kind of writing can't be advocated.
if (p = = q)
...

pointer variable p is not a null pointer judgment:
if (P!= 0)
if (P!= ' ")
if (P!= 3-3)
if (P!= null)/* Use NULL must contain the corresponding standard library header file * *
if (NULL!= p)
if (p)
if (P!= q)
...
can I use the memset function to get a null pointer?

the problem is equivalent to this: 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 on most systems, but because some systems have "non 0 null pointers" (nonzero null pointer), they are not equal in price at this time. For this reason, note that you should not use memset when you want to set the pointer to a null pointer, but you should use a null pointer constant or a null pointer to assign a value to a pointer variable or to initialize a method.
Can you define your own NULL implementation? Can the value of NULL be 1, 2, 3 equivalent? "Similar problems

[7.1.3-2] If The program declares or defines a identifier into a context-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 (reserved identifier) in the standard library that meets the above criteria. Therefore, if NULL is introduced if the corresponding standard header file is included, then it is illegal to redefine null in the program, and its behavior is undefined. In other words, if it is a standard program, its NULL value can only be 0, it is not possible to be other than 0, such as 1, 2, 3, and so on.

Does the malloc function return 0 or NULL when allocating memory failure?

The malloc function is the library function specified in standard C. A "null pointer" (null pointer) is explicitly specified in the standard when its memory allocation fails:

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

For null pointer values, a generic document (such as a man) tends to be expressed in null rather than directly 0. But we should be clear: for pointer types, returning NULL and returning 0 is exactly equivalent, because null and 0 all represent "null pointer" (null pointer). (TYC: null is returned in the manual of the general system, so let's use null.)
Related Article

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.