In depth analysis of global static storage heap and stack areas

  • 2020-04-01 21:25:43
  • OfStack

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 storage area is the system data, we are not free access, sometimes the Windows system will suddenly pop up a message box, the content is "memory can not read" is the result of error access to the system data area; Free storage is used to store the data allocated by the malloc() function that extends from C. The text area holds our function code. When we call a function, the underlying behavior is similar to operating a pointer to the address where the function instruction is located. The const data area, as the name implies, is the memory area where the unmodifiable data is stored, where the const variables we defined are stored. Finally, let's look at the global static storage, heap, and stack areas.

First to see global static storage area, in the program and data are defined by the static label in the global static storage area, both in the main () function outside the definition of a global variable, or the local variables defined in a child function, as long as there is the static label before definition, define after will always exist in the global static storage area. Of course, a global static variable defined outside the main() function can be accessed anywhere, while a local static variable defined in a subfunction is only visible in the module that defines the variable. But there is such a phenomenon: as stated in the front, even if in a child function defined in the local static variables, the form is static, that is to say, as long as after a variable defined statement execution, even where the variables are not visible, as long as to the variable where the address take the parsing operation, also can obtain the value of the variable. So let's say we define a static int a=100 in the function fun(); Assuming that the variable's address is 0x0042AD54, if we take address resolution for 0x0042AD54 after we call fun() in the main() function, we can also get 100: int* p=(int*) 0x0042AD54; Int b = * p; Here b is assigned to 100. From this, we can see that the life cycle of any variable with static definition is the life cycle of the entire program, and the memory occupied by static variables will not be released until the program exits.

The heap storage area behaves like a static storage area. When we allocate memory on the heap, the memory will not be freed automatically without manual freeing. However, in JAVA, there is a mechanism called garbage cleanup that automatically cleans up heap memory, but in C++ there is no such mechanism. That is, in C++, if we allocate heap memory, we must manually free it. Otherwise, if we keep allocating heap memory but don't free it, the program will crash when the memory is exhausted.

Typically, variables allocated with new are stored in heap memory, but returned pointer variables are stored in the stack. Memory leaks occur when we create a new variable in a subfunction, but when the function returns neither a pointer to the new return nor a delete is saved. If we write a server program, the end result of continuous memory leaks is that the server crashes. But in Windows, Linux, and other mature systems, there are similar mechanisms for memory protection. The system will allocate a certain amount of memory for the user program to run, and will also reserve a part of memory for the system's own operation, which is inaccessible to the user program. If we write a program that has a memory leak, when the system runs out of memory allocated to the application, the program will stop running without causing the driver of the system to do so.

As for stack memory, it's also the case that we use the most in writing programs. Every temporary object defined in the program, the pointer returned by new, and the variables in the recursive function are stored in the stack. Stack memory can be released automatically when we define an object in a module, at the end of the module, the memory will be occupied by the variable system recovery, when the new variables, new variables could be deposited in the original place of address, but recovery stack memory in the system, it emits the stack data in memory is not clear, just reset will stack, and when the arrival of the new data will be assigned to the stack.

In C++, although the memory can be freely manipulated, but this technology is like a double-edged sword, good use of sharp, bad use will lead to some puzzling results that they can not understand. A deep understanding of how memory is allocated can be useful for actual programming.

Related articles: