What is a null pointer

Source: Internet
Author: User
Tags define null

The C + + language definition says that each pointer type has a special value----"NULL pointer".
A null pointer is conceptually different from an uninitialized pointer. A null pointer ensures that no object or function is pointed to, and the uninitialized pointer may point to anywhere.
A null pointer is not a wild pointer. Each pointer type has a null pointer, and the different types ofNULL pointerinternal RepresentationMay vary. Although the programmer does not have to know the internal values, the compiler must always be clear about what type of NULL pointers are needed so that they can be differentiated when needed.
1) How to get a null pointer in the program.

According to the language definition, the constant 0 in the context of the pointer is converted to a null pointer at compile time. In other words, when initializing, assigning, or comparing, if one side is a pointer-type value or expression, the compiler can determine that the other side of the constant 0 is a null pointer and generates the correct null pointer value. As shown below:

Char *p=0;if (p!=0)

However, the parameters passed into the function do not necessarily serve as a pointer environment, because the compiler may not recognize the unmodified 0 for "pointers".

In the context of a function call, generating a null pointer requires an explicit type conversion, forcing 0 to be treated as a pointer. For example, a UNIX system can call execl to accept a variable-length character pointer that ends with a null pointer, and it should take the following correct call:

Execl ("/bin/sh", "sh", "-C", "Date", (char*) 0);

If you omit the (char*) conversion of the last parameter, the compiler does not know that this is a null pointer, and it is treated as 0. If there is a function prototype within the scope, the parameter pass becomes an assignment context, which allows the type conversion to be omitted, because the function prototype tells the compiler that a pointer is needed here to correctly convert the unmodified 0 to The appropriate pointer .

It is best to type-convert all null pointers when the function is called.

2) Check if NULL pointer is reliable using "if (p)"

What if the inner expression of a null pointer is not 0? When the C language requires a Boolean value in an expression, the value is assumed to be false if the expression equals 0, otherwise true. In other words, just write

if (expr)


Regardless of whether expr is an expression, the compiler essentially handles it as follows:

if (expr = 0)

If you replace expr with the pointer p, the IF (p) is equivalent to if (P! = 0).
in this comparison context, the compiler can see0is actually aNULL pointer constant, and use the correct null pointer value. There is no deception here, and the compiler works like this, and generates exactly the same code for both. The internal representation of a null pointer does not matter.

So "abbreviations" like if (p) are perfectly legal, but are considered by some to be a bad style.

3) What is null and how is it defined

As a style, many people do not want to see the program in the presence of unmodified 0, so the compiler defines a preprocessing macro in the stdio.h header file null is a null pointer constant.

/* Define NULL pointer value */#ifndef null#ifdef __cpluscplus#define null 0#else#define null ((void *) 0) #endif #endif

As you can see by definition, NULL and 0 are not much different. The compile-time preprocessor restores all null to 0, and the compilation is 0 of the pointer context as described by the context. In particular, a type conversion before NULL is required in a function call.

"Extended reading"

How is null defined on a machine that uses a non-zero null pointer to internally express it?

(3.1) As with other machines: defined as 0 (or some form of 0).

(3.2) When a programmer requests a null pointer, the compiler generates a binary representation that is appropriate for the machine, regardless of whether it writes "0" or "NULL". Therefore, defining NULL as 0 on a machine with a null pointer that is not 0 is as legal as any other machine: the unmodified 0 that the compiler sees in the pointer context produces the correct null pointer.

4) If null and 0 are equivalent to null pointer constants, which one should be used?

Many programmers think that the pointer context should use NULL to indicate that the value should be considered a pointer. Others think that defining 0 with a macro will only complicate things, and they tend to use unmodified 0.

C programmers should understand that NULL and 0 are completely equivalent in the context of pointers, while unmodified 0 can be fully accepted. Any place where null is used should be seen as a gentle pointer hint, but programmers cannot rely on it to differentiate between pointers 0 and integers 0.

"Best practices" although null and 0 have the same functionality, it is recommended to use NULL instead of 0. There are two benefits of this practice:

(4.1) You can assume that the value of NULL has changed, for example, on a machine that uses a non-0 internal null pointer, NULL will be more compatible than 0, but this is not the case.

(4.2) Although symbolic constants often replace numbers to prepare for a change in numbers, this is not a null substitution for 0. The language itself ensures that 0 (for pointer context) in the source code generates a null pointer. Null is used only as a formatting habit.

5) null can be guaranteed to be zero, but null pointers are not necessarily zero

The internal (or pre-run) expression of a null pointer may not be completely zero, and may not be the same for a pointer type that is not used. The real value is only the developer of the compiler cares. The authors of C + + programs never see them, so don't worry, just be clear.


Note

(5.1) The null pointer is not necessarily 0, and Null is definitely 0.

(5.2) A variable assigned to a null pointer ensures that the variable does not point to any object or function. Reasonable use of NULL pointers can effectively avoid memory leaks and improve program execution efficiency.

What is a null pointer

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.