In-depth consideration of Global static storage areas, heap areas, and stack areas

Source: Internet
Author: User
Address: http://gaofeilonglcu.blog.163.com/blog/static/130864291201082084933665/

In C ++, memory can be divided into system data zone, free storage zone, primary zone, const data zone, Global static zone, heap zone and stack zone. System data is stored in the system data zone and cannot be accessed freely. Sometimes a message box pops up in windows, the content is "the memory cannot be read", that is, the result of incorrect access to the system data zone; the free storage zone is used to store the data allocated by the malloc () function extended by C; the handler area stores our function code. The underlying behavior when we call a function is similar to operating a pointer first, and this pointer points to the address of the function instruction, that is, in the handler area; the const data zone, as its name implies, is the memory zone for storing unchangeable data. All the defined const variables are stored here. Finally, let's look at the Global static storage area, heap area, and stack area.

First, let's look at the Global static storage area. In the program, all data defined by the static label is stored in the Global static storage area, whether it is a global variable defined outside the main () function, or the local variables defined in the sub-function, as long as there is a static label before the definition, the definition will always exist in the Global static storage area. Of course, Global static variables defined outside the main () function can be accessed anywhere, and the local static variables defined in the subfunction can only be seen in the module that defines the variable. But there is also such a phenomenon: As described in the front, even if the local static variables defined in the subfunction are static in the form, that is, after a variable-defined statement is executed, the value of the variable can be obtained even if the variable is invisible, as long as the address resolution operation is performed on the address where the variable is located. For example, we define a static int A = 100 in the fun () function. Assume that the variable address is 0x0042ad54. After we call fun () in the main () function, if the IP address of 0x0042ad54 is parsed, you can get 100: int * P = (int *) 0x0042ad54; int B = * P; here, B is assigned 100. From this, we can see that the life cycle of all variables defined by static is the life cycle of the entire program. The memory occupied by static variables will be released until the program exits. The behavior of the heap storage area is similar to that of the static storage area. When we allocate memory on the heap, if we do not manually release the memory, it will not be automatically released. However, in Java, there is a mechanism called garbage cleanup to automatically clean up heap memory, but this mechanism is not available in C ++. That is to say, in C ++, If we allocate heap memory, we must manually release it. Otherwise, if we keep allocating heap memory but do not release it, the program will crash when the memory is exhausted. Generally, the variables allocated with new are stored in the heap memory, but the returned pointer variables are stored in the stack. When we create a new variable in a sub-function, but when the function returns neither the pointer returned by new nor the delete operation, memory leakage will occur. If we write a server program, the final result of Memory leakage will be a server crash. However, in windows, Linux, and other mature systems, there is a mechanism similar to memory protection. The system will allocate a certain amount of memory required for the user program to run, and also reserve some memory for the system's own operations. This part of memory is not accessible to the user program. If the program we write has memory leakage, the program will stop running after the system consumes the memory allocated to the application, without causing the system driver. Stack memory is also the most used in writing programs. Every temporary object defined in the program, pointer returned by new, and variable in recursive functions are stored in the stack. Stack memory can be automatically released. when an object is defined in a module, the memory occupied by the variable will be recycled by the system at the end of the module, when a new variable is defined, the new variable may be stored in the address where the original variable is located, but when the system recycles the stack memory, it will not clear the data in the released stack memory, but will re-adjust the stack top and allocate it to the top of the stack when new data arrives. In C ++, although the memory can be freely operated, this technology is like a double-edged sword. If it is used well, It is very sharp. If it is used badly, it will lead to some inexplicable results that cannot be understood by yourself. A deep understanding of the memory allocation method is helpful for actual programming.

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.