Understanding and processing of wild pointers

Source: Internet
Author: User
Tags define null

Pointer is a powerful tool, but it is not easy to operate because it is too powerful. Improper Operations may cause wild pointers, or even system crashes.

IfProgramIf a pointer is defined, it must be immediately directed to a set space or set to null. If this is not done, the content in the pointer is unpredictable, that is, you do not know which space it points to in the memory (that is, the wild pointer). It may point to a blank memory area, or a protected area, it may even point to the critical memory of the system. If it is so bad, we may accidentally operate the pointer later, which may cause system disorder and crash. So we must set a space to point the pointer to it, or set the pointer to null. What is the principle? If we create a space of the same type as the pointer, in fact, a protected memory space is opened in the blank area of the memory, and then directed to it with a pointer. Then the address in the pointer is the address of the protected space, instead of being unpredictable, we can use pointers to perform corresponding operations on the space. If we set the pointer to null, in the header file definition, we can know that # define null 0. In fact, null indicates 0, so we let the pointer = NULL, in fact, let the pointer = 0, the address (number of machines) in the pointer is initialized to 0, while the memory space with the address 0 is ...... As you can imagine, this address is specific, so it is not an unpredictable wild pointer in the memory.

It should also be noted that free and delete only release the memory referred to by the pointer, but do not kill the pointer itself. After the pointer P is free, its address remains unchanged (not null), but the memory corresponding to the address is junk, and p becomes a "wild pointer ". If P is not set to null at this time, it is mistaken that p is a valid pointer. After free or delete is used to release the memory, set the pointer to null to avoid "wild pointer ". The memory is released, which does not mean that the pointer will die or become a null pointer. (In addition, the pointer disappears and does not mean that the memory it refers to will be automatically released .)

Finally, let's summarize the causes of the wild pointer:
1. pointer variables are not initialized. When a pointer variable is created, it does not automatically become a null pointer. Its default value is random, which means it is random.
2. After the pointer P is free or deleted, It is not set to null, which makes people mistakenly think P is a valid pointer.
3,Pointer operations go beyond the scope of a variable. This situation is hard to prevent.

 

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.