"C + + learning" of 16, about null pointer nulls, wild pointers, universal pointer __c++

Source: Internet
Author: User
Tags define null
First of all, say what is a pointer, as long as you understand the meaning of the pointer, you will understand the meaning of NULL.

Suppose there is a statement int a=10;

The compiler then opens up 1 integer units in memory to hold the variable a, and we assume the address of the integral unit in memory is 0x1000;
So the memory 0x1000 unit holds data 10, and every time we visit a, it's actually 10 of the 0x1000 unit we're accessing.

Now define: int *p;
p=&a;

When the compiler encounters the statement int *p, it also assigns a memory unit to the pointer variable p in memory, assuming that the cell is 0x1003 in memory;
At this point, the value in the 0x1003 is indeterminate, (because we don't assign a value to the pointer), when the compiler encounters the P=&a, it saves the 0x1000 in the 0x1003 cell, see, this means that the memory unit (represented by the pointer variable p) 0x1003 store the memory address of variable A. In layman's terms, p points to variable a.
P=null, that is to say: the memory unit 0x1003 does not hold any variable memory address.

Deletes an array of new. If necessary. such as non-standard class (new CMyClass), in type *p = new Type[n]; delete []p;
At the end of the best add a sentence: p = NULL

A null pointer is a special pointer value and is the only pointer value that is valid for any pointer type. The pointer variable has a NULL pointer value.
It means that it was idle and did not point to something meaningful. The null pointer is expressed in 0, and the C language guarantees that the value will not be the address of any object.
Assigning zero to a pointer value makes it no longer point to anything meaningful. To improve the readability of the program, the standard library defines a 0-equivalent symbolic constant null.
p = 0 can be written in the program; or P = NULL; In both ways, p is placed as a null-pointer value. In contrast, the former is more likely to make the reading process more satisfying.
Recognize that this is a pointer assignment.

We have the impression that the C-language pointer has a type, in fact there is an exception. This involves a generic pointer, which can point to any type of variable.
The type of the generic pointer is represented by (void *) and is therefore called a void pointer.

int n=3, *p;
void *GP;
GP = &n;
p= (int *) GP1;


Wild pointer, which points to a pointer to an unusable memory area. This kind of pointer is usually manipulated, which can cause unpredictable errors to occur in the program.

"Wild Pointer" is not a null pointer, it is a pointer to "junk" memory. People generally do not use null pointers incorrectly, because it is easy to judge with an if statement.
But "wild pointers" are dangerous, and if statements do not work for them. There are two main causes of wild pointers:
One, the pointer variable has not been initialized. Any pointer variable that has just been created does not automatically become a null pointer, and its default value is random, and it can be scrambled.
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.
Second, the pointer p is free or delete, no null, let a person mistakenly thought that p is a legitimate pointer. Although the names of free and delete are vicious (especially delete), they simply release the memory that the pointer refers to, but do not kill the pointer itself. The statement if (p!= NULL) is usually used for error proofing.
Unfortunately, the IF statement does not work correctly, because even if p is not a null pointer, it does not point to a valid block of memory. Cases:


Char *p = (char *) malloc (100);
strcpy (P, "hello");
Free (p); The memory referred to by P is released, but the address indicated by P is still unchanged
if (P!= NULL)//does not play a role in error proofing
strcpy (P, "World"); Error


Another issue to note: Do not return pointers or references to stack memory, because stack memory is freed at the end of the function.
Pointers are a powerful tool, but because it's too powerful, it's not easy to manipulate it. Improper operation caused by the wild pointer, even cause the system crashes and other more serious consequences.

If a program defines a pointer, you must immediately let it point to a space we set or set it to null, and if not,
So the contents of this pointer are unpredictable, not knowing which space it points to in memory (that is, wild pointers), it might point to a blank area of memory that might point to an already protected area, or even to the critical memory of the system, if that's bad, Perhaps we are not careful behind the operation of the pointer can make the system disorder, panic. So we have to set a space for the pointer to point to it, or set the pointer to NULL, this is how a principle, if the same type of space to create a pointer, in fact, in the empty area of memory to open up such a protected memory space, and then point to it with the pointer, Then the address in the pointer is the address of the protected space, not unpredictable, and then we can use the pointer to the corresponding operation of the space;
If we set the pointer to NULL, the #define NULL 0 in the header file definition can be known, in fact, null means 0, so we let the pointer =null, which is actually the pointer = 0, so that the address (machine count) in the pointer is initialized to 0, and the address in memory is 0 Memory space ... Needless to say, you can imagine it, this address is specific, then it is not unpredictable in the memory of the wild pointer.


It should also be noted that free and delete simply release the memory that the pointer refers to, but do not kill the pointer itself. After the pointer p is free its address remains unchanged (not null), only the corresponding memory is garbage, p became "wild Pointer". If P is not set to null at this time, it can be mistaken for a p to be a valid pointer.
After freeing memory with free or delete, you should immediately set the pointer to NULL to prevent the "wild pointer" from being generated. The memory is freed, and does not indicate that the pointer will die or a null pointer. (also, the pointer dies, and does not mean that the memory it refers to is automatically freed.) )


Finally, summarize the origin of the wild pointer: 1, the pointer variable has not been initialized. Any pointer variable that has just been created does not automatically become a null pointer, and its default value is random, and it can be scrambled.

2, the pointer p is free or delete, not set to NULL, let a person mistakenly thought that p is a legitimate pointer.

3, the operation of the pointer beyond the scope of the variable. This situation is impossible to guard against.


2012/10/19

Jofranks in Nanchang

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.