Null pointers and wild pointers in "C + + Advanced" C + + __c++

Source: Internet
Author: User
Tags reserved
NULL pointer Constants

An integer constant representing a value of 0, called a null pointer constant. For example: 0, 0L, 1-1 (both are integer constant expressions with a value of 0) and (void*) 0, void* null are NULL pointer constants, NULL pointer constants can be assigned to any pointer type because it is a variant type (void*). But we are more inclined to use NULL to represent this null pointer constant. For other methods, such as 0, the null pointer constant does not produce any problems, but does not in the fundamental sense conform to the definition of NULL pointer constants. Because the existence of NULL pointer constants is still stressed that it does not point to any object (details later). null pointer null pointer does not point to any actual object or function. Conversely, the address of any object or function cannot be a null pointer. A null pointer is a special pointer, because the pointer does not point anywhere. This means that any valid pointer, if compared to an equal comparison operation with a null pointer, will result in false. In a program, the most straightforward way to get a null pointer is to use a predefined null, which is defined in multiple header files. If you want to initialize a null pointer, we can do this,

int *ip = NULL;
We prefer to use this method when verifying whether a pointer is a valid pointer
if (IP!= NULL)
Instead of
if (IP)
Why is it that someone uses the if (IP) method to verify that a pointer is non-null and that there is no error in C + +? And now a lot of people will write that. The reason is this,
Define   NULL   pointer   value 
#ifndef   null 
#   ifdef   __cplusplus 
#     Define   null      0 
#   Else 
#     define   null      ((void   *) 0) 
#   EndIf 
#endif//   
NULL is defined in today's C + + as 0, and true in C + + is ≠0, so the IF (IP) and if (IP) at this time. = NULL) is equivalent.
NULL pointerNULL is a standard defined macro definition used to represent null pointer constants. In C + + it is directly defined as 0 of the immediate number of integers, and without the __cplusplus definition, it is defined as a void* type pointer constant with a value of 0. 0 PointersA 0-value pointer, a pointer to a value of 0, can be any type of pointer, can be a generic variant type void*, or it can be char*, int*, and so on. In C + +, any concept is expressed in the form of a language memory recognition, for example, Std::vector provides a empty () child function to return whether the container is empty. However, for a basic numeric type (or just a type similar to an integer type), it is not possible to abstract it into a class (except, of course, smart pointers such as AUTO_PTR) to provide a detailed description of the state, so we need a special value to perform this state. The C + + standard stipulates that when a numeric value of a pointer type is 0 o'clock, the pointer is considered empty. (We may be able to use other special values to define the null implementation we need, which can be 1, or 2, depending on the implementation requirements, but under standard C + + we use 0来 to implement null pointers) where the null pointer points to memoryThe standard does not specify where the null pointer points to memory, that is, which specific address value represents a null pointer depending on the system implementation. 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); There are also systems that represent null pointers (nonzero null pointer, not 0 null pointers) in special address values or in special ways, see C FAQ. In implementing 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 representation of an empty pointer equivalent to an integer 0--sometimes they are different, as described above. protection policy for null pointer implementation logical address and Physical addressNow that we have chosen 0 as the concept of emptiness. We need protection and error when illegally accessing the space. As a result, compilers and systems provide a good policy. The pointer in our program is actually the address of the Windows memory segment offset, not the actual physical address, so the 0-value pointer in the different address points to the same 0 address, in fact in memory is not the beginning of physical memory 0, but the beginning of segmented memory, Here we need to briefly describe the memory allocation and management system under Windows: Under Windows, the execution file (PE file) is called, and the system assigns it a rated memory segment to map all the contents of the program (that is, the content on the disk) and to perform a new offset calculation for this segment , which means that all the near pointers we access in our programs are in our "Home" segment, and when we need to access the far pointer, we actually jump out of the "home yard" to someone else's place, We need a segment offset qualification to complete the new offset (home offset) So our pointer may be oe02:0045 is to tell us to access the 0E02 memory segment of the No. 0045 offset, and then Windows will automatically find us a 0E02 segment of the start offset, Then compute the real physical address for us. So the 0 value pointer in program A and the 0 value pointer in program B may be completely different. null pointer assignment partitionThis partition is the process's address space from 0x00000000 to 0x0000ffff in the closed range (64K of memory size), this 64K memory is a piece of reserved memory, can not be allocated by the program dynamic memory allocator, can not access, and can not be used, The partition is reserved for the purpose of helping the programmer to capture the assignment of null pointers. An access violation is raised if a thread in the process attempts to read or write to a memory address located in that partition. Why null pointer access can cause exceptionsIn the final analysis, the data used in the program needs to be obtained from the physical device, that is, the data in the program needs to be read or written from a real physical address. So when a pointer's logical address can be accurately mapped to a correct physical address, the data is accessed correctly and the program executes without any problems. If a pointer is a null pointer, the logical address space that the pointer points to is located on the interval of the null pointer assignment partition. The logical address on the null pointer assignment partition does not correspond to the physical memory, and thus accesses an exception that is accessed illegally. Wild PointerThe wild pointer is not a null pointer, it is a pointer to garbage memory. Cause of formation 1. The pointer variable has not been initialized. Any pointer variable is not automatically initialized to a null pointer when it is first created, and its default value is random. Therefore, the pointer variable should be initialized at the time it is created, either by setting the pointer to null or by having it point to legitimate memory. For example:
char* p = NULL;
char* str = (char*) malloc (1024);
2. After the pointer is free or delete, it is not set to NULL, which is mistaken for a legitimate pointer. Free and delete Simply release the memory pointed to by the pointer, but do not clear the pointer itself. At this point the pointer still points to the original location, except that the memory data in this location has been destroyed, at this point the pointer to the memory is a garbage memory. But at this point the pointer is not a null pointer (without null), and when you do the following pointer checksum
if (P!= NULL)
Will escape the checksum, the p is not a null pointer and does not point to a valid block of memory, causing the failure of pointer access in the meeting program.
3. The pointer operation goes beyond the scope of the variable. Due to the + + operation of the pointer in C + +, so in the execution of this operation, a little careless, easy pointer access to the cross-border, access to a memory should not be accessed, resulting program crashes the other is a reference to a temporary variable, and when the variable is released, the pointer becomes a wild pointer, as follows
A *p; A is a custom object
 {
     a A;
     p = &a; Notice the lifetime of a, only in this block (two lines inside the curly braces), not the entire test function
  }
  p->func ();  P is "wild pointer"


Note: This article refers to click on the Open link

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.