Local variables, global variables, stack, stack, static and global difference

important

111

Seen from the figure, the program memory is divided occupied following sections.

1, the stack area (Stack)

Automatically allocated by the compiler release, storage parameter value of the function, local variables, etc., memory allocation is continuous, usually similar to what we call stack, it is unclear if, then put it into an array of like, it memory allocation is allocated contiguously, i.e., the memory is allocated in a contiguous memory area. When we declare a variable, then the compiler will automatically then the end of the current stack area of ​​memory to allocate.

2, the heap area (heap)

General distribution release by the programmer, if the programmer does not release, the operating system may be recovered by the end of the program. Is similar to the list, the distribution is not continuous in memory, which are regions of different memory blocks linked by the pointer. Once a node is disconnected from the chain, as we want to disconnect the artificial node is released from memory.

3, global area (static area) (static)

Global variables and static variables are stored on a piece of initialization of global variables and static variables in an area, uninitialized global variables and uninitialized static variables in an area adjacent to another. After the end of the program released by the system

4, literals area

Constant string is placed here. After the end of the program released by the system

5, the program code area

Binary code storage function body.

Look at an example.

char c; // allocated on the stack

char * p = new char [3]; // allocated on the heap, the address assigned to the p;

 

 

 

When the compiler encounters the first instruction to calculate its size, and then to find space in the current stack is greater than the amount of space required for allocation, if the time within the stack space than the space application, then assign memory space Note: here, the distribution of internal space is continuous and is followed by the distribution of the end of the last assignment. If the size of the stack space is smaller than the space application, then the system will then reveal the stack overflow, and the corresponding abnormality information. ----- kernel crash

 

When the compiler encounters the second instruction, since p is allocated on the stack, so when p is assigned the internal space and the same way as above, but encountered new keyword, the compiler knows that this is a user application of dynamic memory space, so it will go to heap up its search for space allocation. Attention: heap memory space is not continuous, it is appropriate to list the internal space of the block at the time of its connection zone, so after receiving the specified allocation of memory space, it does not immediately assign the appropriate space, but must first calculate the required space, and then to the stack over the entire column (i.e. the column node over the entire chain), the memory blocks allocated to the first encountered it. And finally the first address in the heap-allocated array of characters assigned to p., This time, we have clear, p is now stored in the first address array of characters in the heap of the application, the application is in the heap address of the array is now assigned to the pointer variable p on the stack application. To illustrate the problem more image, see below:

111

As can be seen from the chart, we first address dynamically allocated on the heap array of content stored in the pointer p points.

Please note: requested on the stack memory space, when we out of the scope of the variable is located, the system will automatically We recycle these spaces, and in the application heap space, and when the appropriate scope in the future, we need to explicitly call delete to free memory space application, if we do not get timely release of these space, then the memory of memory fragmentation on more and more, so our actual memory space will become more and more less, that is, memory blocks more and more isolated. Here, we know that the heap memory area is not continuous, or after the effective memory area pointer linked list, if we apply to a certain piece of memory, then this will be a memory area from a continuous (via list up) off the block of memory, if we use finished, it is not timely to be released, it will open in isolation, because there is no pointer to it, so this area will become a memory fragmentation, so after use of dynamically allocated memory (NEW by applications), it must be explicitly deleted dELETE. For this, we must remember. . .

The above stated concept for everyone between them, compare them to use both aspects, and here I will not be able everyone intermittent stated, for that matter, a netizen online articles set forth in detail, but also comes with professional color the following article is part of the piece.

 

Application size limit

Stack: In Windows, the stack is expanded to the low address data structure is a contiguous area of ​​memory. This means that the maximum capacity of the stack address and stack predetermined system is good, in WINDOWS, the stack size is 2M (some say 1M, in short, it is a compile-time constants determined), if when space applications exceeds the remaining space on the stack you will be prompted to overflow. Therefore, less space obtained from the stack.

Heap: heap is extended to the high-address data structure is not continuous memory area. This is because the system is a linked list of free memory for storing the address of a discontinuous nature, and the list traversal direction is from low to higher addresses. The heap size is limited by the computer system effective virtual memory. Thus, the space available heap more flexible, is relatively large.

Application efficiency comparison:

Stack is automatically assigned by the system faster. But the programmer can not control.

Heap is a new memory allocation, generally more slowly, and prone to memory fragmentation, but it most convenient to use.

In addition, in WINDOWS, the best way is to use VirtualAlloc to allocate memory, he was not in the heap, nor is it a fast memory is reserved in the stack directly in the address space of a process, although it used the most inconvenient. But the speed, but also the most flexible.

Heap and stack memory contents in

Stack: the function call, the first into the stack is the next instruction in the main function (executable statement at a function call statement) address and is a function of various parameters, in most compilers C , the argument is pushed onto the stack from right to left, then a function of the local variables. Note that the static variable is not pushed on.

After the end of this function call, first-out stack of local variables, then the parameters of the last stack pointer points to the beginning address of the memory, i.e. the main function of the next instruction, the program continues to run from this point.

Heap: one byte is generally stored in the heap size of the head stack. The specific content of the heap programmers arrangements.

 

Guess you like

Origin blog.csdn.net/ll148305879/article/details/94391118
Recommended