How to visualize the explanation of memory leaks and wild pointers

Source: Internet
Author: User

Memory leaks: The first thing to say is how to request a piece of memory space for a variable.

The malloc function is required when you want to request a memory space for variable A. How to apply for this memory space. This becomes the work of the program. The program first applies a piece of memory space to the system, and then the program will assign the first address of the requested memory space to the variable A, assuming that the variable a gets the first address is 0x1234, if we now want to loop the variable A, then each loop, variable a will get a new first address, so that , the memory space is continuously assigned to variable a.

But now it is important to note that every time you get a new first address, the old first address will be overwritten, but the use of the old memory space is still in the hands of the program. There are two concepts, systems and procedures to be clearly divided. Usually, it is the application of the program to the system to apply for space, the allocation of space in the system hand. Although the old address Bai covered, but the right to use is still in the hands of the program, so the program can be used less space, resulting in insufficient memory and leaked.

Wild pointer: When the program to the system to apply for a piece of memory space, the program will be the application number of this memory space of the first address assigned to the variable A, assuming that the first address of the variable A is 0x3456, then when the space used to complete the release of free, the use of this space is the program back to the system, But this time if the variable A is not assigned a null value, then the variable A in the hand to get still the first address 0x3456, this address is not destroyed, so since it has a first address, then can point to an address, but because the address of the memory space is not the right to use the program , therefore, the pointer operation of variable A is illegal at this time, and such pointers are called wild pointers.


Here I use a metaphor to illustrate what a memory leak is and what a pointer is.

Zhang San refers to the variable A; The property card refers to the use of memory space, the key refers to the first address of the memory space.


When Zhang San get the House property card and key of 1, this time he can use this House 1,




After a period of time, Zhang San and bought a house, we referred to as the House 2, but this time Zhang San the House 1 of the key to lose, because the property is still Zhang San, so others can not live this House, but because Zhang San no key, so he can not enter the House 1, the House 1 is so empty.



And so on, when Zhang San bought a lot of houses, but because he lost the key can not live, and others because there is no house certificate, so also can't live, so these houses are empty down. When the vacant house is too large, it becomes a memory leak.



When Zhang San get the House's keys and property, when the property is free (memory is released), it is tantamount to saying that the property is lost.


This time if the variable a does not have a null value, it is equal to say that the key is still in the Zhang San Hand (the first address is also in the variable a), so although the key is still in the hands of Zhang San, but there is no property, he entered the house into the illegal entry, this is the wild pointer.


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.