Deep analysis of _c language in global static storage area, heap area and stack area

Source: Internet
Author: User
In C + +, memory can be divided into system data area, free storage area, text area, const data area, global static area, heap area and stack area. Among them, the system data area is stored system data, we are not free access, sometimes Windows system will suddenly pop up a message box, the content is "memory can not be read" is the error access to the system data area results; Free storage is used to store malloc () extended by C. The data allocated by the function; The text area holds our function code, and the underlying behavior when we call the function is similar to manipulating a pointer, which points to the address of the function instruction, which is in the text area; The const data area, as the name implies, is the memory area where the data cannot be modified. The const variables we define are stored here. Finally, we look at the global static storage area, heap area and stack area.

First look at the global static store, in which the data defined by the static label is stored in a global static store, whether it is a global variable defined outside the main () function, or a local variable defined in a child function, as long as there is a static label before the definition, Once defined, it will always exist in the global static storage area. Of course, a global static variable defined outside of the main () function can be accessed anywhere, while a local static variable defined in a child function is visible only in the module that defines the variable. But there is also a phenomenon: As mentioned earlier, even if a local static variable is defined in a child function, its existence form is static, that is, the value of the variable can be obtained only if the variable is not visible, even if the variable is invisible. For example, we define a static int a=100 in the function fun (), assuming that the address of the variable is 0x0042ad54, and after we call Fun () in the main () function, we can get 100 if the address is parsed for 0x0042ad54: int* p = (int*) 0x0042ad54; int b=*p; Here B is assigned a value of 100. Thus, we can see that the life cycle of a variable with a static definition is the lifecycle of the entire program, and the memory occupied by the static variable is released until the program exits.

Heap storage behaves like a static store, and when we allocate memory on the heap, the memory is not automatically released without manual release. But in Java, there is a mechanism called garbage cleanup that automatically cleans up heap memory, but there is no such mechanism in C + +. In other words, in C + +, if we allocate heap memory, we must release it manually. Otherwise, if we keep allocating heap memory, but do not release it, when the memory is exhausted will cause the program to crash.

Generally, variables assigned with new are stored in heap memory, but the returned pointer variables are stored on the stack. When we new a variable in a child function, it can cause a memory leak when the function returns without either saving the pointer returned by the new or the delete. If we are writing a server program, the end result of constant memory leaks is the server panic. But in Windows, Linux, and other mature systems, there are mechanisms similar to memory protection. The system will give the user program to allocate a certain amount of memory required to run, the same will also give the system itself to run a part of the memory, this part of the memory is not accessible by the user program. If we write a program that has a memory leak, the program stops running without causing the driver of the system, after exhausting the memory allocated to the application by the system.

As for stack memory, it is the most common case we use to write programs. Each temporary object that is defined in the program, the pointer returned by new, and the variables in the recursive function are stored on the stack. Stack memory can be automatically released, when we define an object in a module, at the end of the module, the memory occupied by the variable is reclaimed by the system, and when the new variable is defined, the new variable may be stored on the same address as the original variable, but when the system reclaims the stack memory, is to not empty the data in the freed stack memory, but simply readjust the top of the stack and assign it to the top of the stack when new data arrives.

In C + +, although the free operation of memory, but this technology is like a double-edged sword, with a good sharpness, using a bad instead will cause some of their own can not understand the inexplicable results. An in-depth understanding of how memory is distributed can be useful for practical 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.