Function of pointer initialization to NULL

Source: Internet
Author: User
Tags define null

About null pointers, wild pointers, general pointers, first of all, what is a pointer, as long as you understand the meaning of the pointer, you understand the meaning of NULL.

Suppose there is a statement int a=10;
Then the compiler opens up 1 integer cells in memory A, we assume that this integer unit in memory address is 0x1000, then the memory 0x1000 unit is stored in the data 10, each time we access a, is actually access to the 0x1000 unit 10.
Now defined: 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 which point the value in 0x1003 is indeterminate (because we do not assign a value to the pointer), and when the compiler encounters the P=&a, Will save the 0x1000 in the 0x1003 unit, see, this is to say: (pointer variable p represents) memory unit 0x1003 Store the memory address of variable a! In popular parlance, p points to variable a.
P=null, that is to say: Memory unit 0x1003 does not store any variable memory address.

Deletes an array of new. If necessary. For example, non-standard classes (new CMyClass), in type *p = new Type[n]; delete []p; the last better one: p = NULL

A null pointer is a special pointer value and the only pointer value that is valid for any pointer type. A pointer variable has a NULL pointer value, indicating that it was idle at the time and did not point to something meaningful. The null pointer is represented by 0, and the C language guarantees that the value will not be the address of any object. Assigning a pointer value to zero makes it no longer point to anything meaningful.    To improve the readability of the program, the standard library defines a symbolic constant that is equivalent to 0 null.     The program can write P = 0; or P = NULL; Both of these formulations put p as null pointer values. In contrast, the former makes it easier for people who read the program to realize that this is a pointer assignment.

We have the impression that the C language pointer has a type, there is actually an exception. This involves a generic pointer, which can point to any type of variable. The type of a generic pointer is represented by (void *) and is therefore also known as a void pointer.
int n=3, *p;
void *GP;
GP = &n;
p= (int *) GP1;

The wild pointer, which is a pointer to an area of unavailable memory. The usual manipulation of such pointers will cause unpredictable errors in the program.
The "Wild pointer" is not a null pointer, it is a pointer to "junk" memory. It is generally not wrong to use a null pointer because it is easy to judge with an if statement. But a "wild pointer" is dangerous, and the IF statement does not work on it. There are two main causes of wild pointers:

One, the pointer variable is not 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 arbitrary. Therefore, the pointer variable should be initialized at the same time it is created, either by setting the pointer to null or by pointing it to legitimate memory.

Second, after the pointer p is free or delete, it is not set to NULL, which makes the person mistakenly think P is a valid pointer. Although the names of free and delete are ferocious (especially delete), they simply release the memory that the pointer refers to, but do not kill the pointer itself. Usually, the statement if (P! = NULL) is used for error-proof handling. Unfortunately, the IF statement is not error-proof at this point 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 freed, but the address referred to by P remains unchanged

if (P! = NULL)//does not play an anti-error role

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. The
pointer is a powerful tool, but because it is too powerful, it is not easy to manipulate it.  Improper operation caused by the wild pointer, even caused by the system crashes and other more serious consequences. If the program defines a pointer, it must immediately point to a space we set or set it to null, if not, then the contents of this pointer is unpredictable, that is, it does not know which space in memory (that is, the wild pointer), it may point to a blank memory area, It might point to an already protected area, or even to the critical memory of the system, and if that's the case, maybe we're going to be careless about the pointer and it might cause the system to go haywire and crash. So we have to set a space for the pointer to point to it, or to set the pointer to NULL, this is how a principle, if it is to create a pointer to the same type of space, in fact, in the memory of an empty area to open up a protected memory space, and then with a pointer to it, Then the address in the pointer is the address of the protected space, not the unpredictable, then we can use the pointer to the space to do the corresponding operation, if we set the pointer to null, we in the header file definition of the #define NULL 0 can know, in fact, null means 0, So we let the pointer =null, actually let the pointer = 0, so, the pointer in the address (number of machines) is initialized to 0, and the memory address is 0 of the memory space ...   Needless to say also can imagine, 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 the same (not NULL), except that the memory corresponding to the address is garbage, and P becomes the "wild pointer". If P is not set to null at this point, the person will mistakenly assume that P is a valid pointer. Once you have freed memory with free or delete, you should immediately set the pointer to NULL to prevent the "wild pointer" from being produced. The memory is freed and does not indicate that the pointer is extinct or is a null pointer. (and, if the pointer dies, it does not mean that the memory it refers to is automatically freed.) Finally, summarize the origin of the wild pointer: 1, the pointer variable is not 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 arbitrary. 2. After the pointer p is free or delete, it is not set to NULL, which makes the person mistakenly think P is a valid pointer. 3, the pointer operation goes beyond the scope of the variable. This situation is very impossible to guard against.

Pointer is initialized to NULL

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.