Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C++ tutorial: NULL pointer, zero pointer, wild pointer

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

C++ tutorial: NULL pointer, zero pointer, wild pointer

1. Null pointer, NULL pointer, zero pointer

1.1 what is a null pointer constant

0, 0L, ", 3-3, 0 * 17 (they are all" integer constant expression ") and (void*) 0 (I think (void*) 0 should be regarded as a null pointer, more appropriately) are null pointer constants (note that (char*) 0 is not a null pointer constant, just a null pointer value). As for which form the system chooses to use as the empty pointer constant, it is relevant to the implementation. General C system choice (void*) 0 or 0 in the majority (there are individual choices of 0L); as for C++ system, due to strict requirements for type conversion, void* can not be freely converted to other pointer types as in C, so usually choose 0 as the null pointer constant (recommended by C++ standard) rather than (void*) 0.

1.2 what is a null pointer

If p is a pointer variable, then p = 0; p = 0L; p = "; p = 3-3; p = 0 * 17; after any assignment operation (or p = (void*) 0 for C), p becomes a null pointer, which 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. (for example, (void*) 0 here is a null pointer. It certainly doesn't matter that there is a microsecond difference between null pointer and null pointer constant. In fact, a null pointer is just a programming concept, just as a container may have two basic states: empty and non-empty.

1.3 NULL pointer

NULL is a standard macro definition used to represent an empty pointer constant. Therefore, in addition to the various assignments above, you can also use p = NULL; to make p a null pointer.

(the implementation in many systems: # define NULL (void*) 0 is not exactly the same as the "a null pointer constant" here.)

NULL pointer defined by C++ Standard Library

/ / Define NULL pointer value

# ifndef NULL

# ifdef _ _ cplusplus

# define NULL 0

# else

# define NULL ((void *) 0)

# endif

# endif / / NULL

NULL is a macro that is directly defined as an integer immediate number type 0 in C++ and, in the absence of a _ _ cplusplus definition, as a void * type pointer constant with a value of 0.

1.4 Zero pointer

A zero-value pointer, which is a pointer with a value of 0, can be any pointer type, can be a general variant type void*, can be char*,int*, and so on.

In C++, any concept has to be expressed in a recognized form of language memory. For example, std::vector will provide an empty () subfunction to return whether the container is empty. However, for a basic numeric type (or just a type similar to an integer type), it is impossible to abstract it into a class (except for auto_ptr, of course, only pointers) to provide a detailed description of its status. So we need a special value to represent this state.

The C++ standard states that when the value of a pointer type is 0, the pointer is considered empty. (we may be able to use other special values to define the NULL implementation we need under other standards, which can be 1 or 2 depending on the implementation requirements, but under the standard C++ we use 0 to implement the NULL pointer)

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

The standard does not specify where the null pointer points to memory, that is, which specific address value (0x0 address or a particular address) to indicate that the null pointer depends on the implementation of the system. Our common null pointer generally points to the 0 address, that is, the inside of the null pointer is represented by all zero (zero null pointer, null pointer); there are also some systems that use some special address values or special ways to represent the null pointer (nonzero null pointer, non-zero null pointer), see C FAQ for details.

In actual programming, we don't need to know whether the pointer is a zero null pointer or a nonzero null pointer over our system, we just need to know whether a pointer is a null pointer-the compiler will automatically implement the conversion, shielding 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 mentioned above, sometimes they are different.

1.6 Protection policy for null pointer implementation

Since we have chosen 0 as the concept of null, we need to protect and report errors when illegally accessing null. As a result, compilers and systems provide good policies.

The pointer in our program is actually the offset address of the WINDOWS memory segment, not the actual physical address, so the zero pointer in different programs points to the same 0 address. In fact, the zero pointer in memory is not the beginning of physical memory, but the beginning of segmented memory. Here we need to briefly introduce the memory allocation and management system under WINDOWS:

Under WINDOWS, after the execution file (PE file) is called, the system will assign it a rated memory segment to map all the contents of the program (that is, the contents on disk) and calculate a new offset for this segment, that is, all the NEAR pointers accessed in our program are in our "home" segment, when we want to access the FAR pointer. We actually jumped out of the "own yard" to someone else's place, and we need a segment offset address to complete the new offset (the offset in someone's home), so our pointer may be OE02:0045, which tells the system that we want to access the 0045 good offset of the 0E02 memory segment, and then WINDOWS will automatically find the start offset of the 0E02 segment for us, and then calculate the real physical address for us.

So the zero pointer in program A may point to a completely different place from the zero pointer in program B.

Protection Policy:

Our program is using a segment given by the system, and the zero pointer in the program points to the beginning of this segment. In order to ensure the concept of NULL, the system has made stringent regulations for the beginning of 64K memory of our segment. According to the access control of virtual memory, access to 64K memory that requires high access rights in our program (low access rights) is regarded as not allowed, so it will inevitably lead to Access Volitation errors. This high-permission 64K memory is a piece of reserved memory (that is, it cannot be allocated by the program dynamic memory allocator, cannot be accessed, and cannot be used), which is simply reserved without any use.

After defining a pointer directly, we don't know where it points to (rather than some programmers think that languages such as JAVA will automatically initialize zero), so once we illegally access these unknown content directly, we are very likely to touch the memory that the program cannot touch. (at this time, protection policies such as 64K restrictions will take effect again. Just as you not only break into a stranger's home (wild pointer) at will, but also ask him for money (visit) with a knife, the police (WINDOWS memory access protection policy) of course ask you to go to the police station (report an error) to talk, so it is necessary to develop a good pointer initialization (assigned to NULL) and initialize it to null immediately after using FREE (or when DELETE)!

1.7 Why does an exception occur when reading and writing through a null pointer?

Partition assigned by the NULL pointer: its range is from 0x00000000 to 0x0000FFFF. This space is free, for free space, there is no corresponding physical memory corresponding to it, so for this space, any read and write operation will cause an exception. A null pointer is the address of a program that does not have physical memory at any time. In order to guarantee the "whenever" condition, it is necessary to artificially divide a null pointer area, which is inherently partitioned by the NULL pointer above.

1.8 can you define your own implementation of NULL?

NULL is a reserved identifier (reserved identifier) in the standard library. Therefore, if the corresponding standard header file is included and NULL is introduced, it is illegal to redefine NULL as different content in the program, and its behavior is undefined. In other words, if it is a standard program, the value of NULL can only be 0, and it cannot be anything other than 0, such as 1, 2, 3, and so on.

1.9 does the malloc function return 0 or NULL when memory allocation fails?

The malloc function is a library function specified by standard C. It is specified in the standard that a "null pointer" (null pointer) is returned when its memory allocation fails. For null pointer values, general documents (such as man) tend to be expressed as NULL rather than 0 directly. 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). (NULL is returned in manuals in general systems)

If the NEW in C++ fails to re-memory, a BAD_ALLOC exception will be thrown.

two。 Wild pointer

The "wild pointer" is not a NULL pointer, but a pointer to "junk" memory.

2.1 there are two main causes of "wild pointer".

1) the pointer variable is not initialized. Any pointer variable will not automatically become a NULL pointer when it is created. Its default value is random and it will point at random. Therefore, the pointer variable should be initialized at the same time as it is created, either setting the pointer to NULL or letting it point to legitimate memory. For example:

Char * p = NULL

Char * str = (char *) malloc

2) after the pointer p is free or delete, it is not set to NULL, making people think that p is a legal pointer.

Free and delete just freed the memory referred to by the pointer, but did not kill the pointer itself. After free, its address remains the same (not NULL), but the memory corresponding to that address is garbage, and p becomes a "wild pointer". If p is not set to NULL at this time, it will give the impression that p is a legal pointer. If the program is long, we sometimes can't remember whether the memory referred to by p has been freed. Before continuing to use p, we usually use the statement if (p! = NULL) to prevent errors. Unfortunately, the if statement does not protect against errors at this time, because even if p is not a NULL pointer, it does not point to a legitimate block of memory.

Char * p = (char *) malloc

Strcpy (p, "hello")

The memory referred to by free (p) / p is freed, but the address referred to by p remains the same.

...

If (p! = NULL) / / does not play the role of error prevention

{

Strcpy (p, "world"); / / error

}

3) pointer operation is beyond the scope of variables. This situation is overwhelming. The example program is as follows:

Class A

{

Public:

When void Func (void) {cout Func (), the object a has disappeared, and p points to a, so p becomes the "wild pointer".

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report