C++ heap and stack differences and relationships

  • 2020-06-15 09:56:31
  • OfStack

In C++, memory is divided into five areas: heap, stack, free storage, global/static storage, and constant storage.

Stack: A variable store that is allocated automatically by the compiler when needed and cleared automatically when not needed. Usually store local variables, function parameters, etc. Heap: A block of memory allocated by new and released by the programmer (regardless of the compiler), usually 1 new corresponds to 1 delete, 1 new[] corresponds to 1 delete[]. If the programmer does not release, the resource is automatically collected by the operating system at the end of the program. Free storage: A block of memory allocated by malloc et al., similar to heap 10, freed by free. Global/static storage: Global and static variables are allocated in the same block of memory (in C, global variables are divided into initialized and uninitialized, which is not distinguished in C++). Constant storage: This is a special storage block that holds constants and is not allowed to be modified.

(Note: Heap and free storage are actually the same block, malloc is called in the underlying implementation code of new, and new can be regarded as the advanced version of malloc's intelligence)

1. Heap and stack discussion:

Management: Resources in the heap are controlled by the programmer (easy to generate memory leak), stack resources are managed automatically by the compiler without manual control. System response: to heap, should know that the system has a record of free memory address list, when the system is receiving program application, traversing the list, find the first space is greater than the application space of nodes, delete in addition to the free nodes, the nodes in the list and assign the node space program (most systems will be in the first address the memory space distribution of size, in this way can delete right to release the memory space, and system will spare part back into the free list). For the stack, the system provides the program with memory as long as the remaining space of the stack is greater than the applied space; otherwise, an exception prompts the stack out. Space size: The heap is a discontinuous memory area (since the system USES linked lists to store free memory addresses, it is not continuous). The heap size is limited by the virtual memory available in the computer system (32bit system is theoretically 4G), so the heap space is flexible and large. The stack is a contiguous area of memory, the size of which is predetermined by the operating system, and the stack size under windows is 2M (or 1M, which is determined at compile time and can be set in VC). Fragmentation problem: For the heap, frequent new/delete causes a lot of fragmentation, reducing program efficiency. For the stack, it is an in and out queue, 11 in and 11 out, no fragmentation. Direction of growth: heap upward, growing toward high address. The stack goes down and grows towards the lower address. Allocation: Heaps are dynamically allocated (heaps without static allocation). The stack has static allocation and dynamic allocation. The static allocation is done by the compiler (such as local variable allocation), while the dynamic allocation is done by the alloca function. However, the dynamically allocated resources of the stack are released by the compiler without the programmer. Allocation efficiency: The heap is provided by the C/C++ library and the mechanism is complex. So the heap is much less efficient than the stack. The stack is a data structure provided by the system. The computer provides support for the stack at the bottom level. It allocates a special register to store the address of the stack.

2. Program example

The above concepts can be better understood through the following program.


int  b;  
//main.cpp
int  a  =  0;  // Initialize the global area 
char  *p1;    // Global uninitialized area 
main(){int  b;           // The stack 
char  s[]  =  "abc";   //  The stack 
char  *p2;         // The stack 
char  *p3  =  "123456";  // 123456/0 In the constant region, p3 On the stack. 
static int c = 0 ;       //  Global (static) initialization area 
p1 =  (char *)malloc(10)
p2 =  (char *)malloc(20)  //  Well distributed 10 and 20 The region of bytes is in the heap region. 
strcpy(p1, "123456");    // 123456/0 Put in a constant area, which the compiler might associate with p3 Points to the "123456" optimized 1 A place. 
}

Summary:

The difference between a heap and a stack can be seen in the following analogy:

Using a stack is like going to a restaurant to eat. We order (send out the request), pay, and eat (use). When we eat, we leave. Use heap to be like to do by oneself the dish that likes to eat, more troublesome, but more accord with oneself taste, and freedom is big. (Classic!)

conclusion


Related articles: