C++ Stack and Stack Difference and Connection Explanation


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

  • Stack: A variable store that is automatically allocated by the compiler when needed and automatically cleaned up when not needed. Local variables and function parameters are usually stored.
  • Heap: a block of memory allocated by new, released by the programmer (regardless of the compiler). Generally, a new corresponds to a delete, and a new [] corresponds to a delete []. If the programmer is not released, resources will be automatically recovered by the operating system after the program is completed.
  • Free Storage: A block of memory allocated by malloc, which is very similar to the heap and is freely released.
  • Global/static storage: Global variables and static variables are allocated to the same block of memory (in C language, global variables are divided into initialized and uninitialized, which is not distinguished in C++).
  • Constant Storage Area: This is a special storage area where constants are stored and no modification is allowed.

(Note: The heap and the free storage area are actually the same area. Malloc is called in the underlying implementation code of new, which can be seen as an advanced version of malloc intelligentization.)

The discussion of stack and stack:

  • Management mode: The resources in the heap are controlled by programmers (easy to generate memory leak), and the stack resources are managed automatically by compilers without manual control.
  • System Response: For heap, you should know that the system has a list of free memory addresses. When the system receives a program application, it traverses the list, searches for the first heap node whose space is larger than the application space, deletes the node in the free node list, and allocates the node space to the program (most systems record the allocation at the first address of the memory space). Size, so delete can release the memory space correctly, in addition, the system will put the redundant part back into the free list. For the stack, as long as the remaining space of the stack is larger than the requested space, the system provides memory for the program, otherwise the stack will be prompted by an exception.
  • Space size: The heap is a discontinuous memory area (because the system uses a linked list to store free memory addresses, naturally not continuous), and the heap size is limited by the effective virtual memory in the computer system (32bit system is theoretically 4G), so the heap space is more flexible and larger. The stack is a continuous memory area. The size of the stack is predetermined by the operating system. The size of the windows stack is 2M (1M is also determined at compile time, and can be set in VC).
  • Fragmentation problem: Frequent new / delete can cause a lot of fragmentation for the heap, which reduces the efficiency of the program. For stack, it is a queue of first-in, last-out, one-to-one correspondence and no fragmentation.
  • Growth direction: heap up, to the direction of high address growth. The stack goes down and grows toward a lower address.
  • Assignment: The heap is dynamically allocated (there is no static allocated heap). Stack has static allocation and dynamic allocation, static allocation is completed by compiler (such as local variable allocation), dynamic allocation is allocated by alloca function, but dynamic allocation of resources is released by compiler without programmer.
  • Allocation efficiency: The heap is provided by C/C++ function library, and the mechanism is very complex. So the efficiency of the stack is much lower than that of the stack. The stack is the data structure provided by the extreme system. The computer supports the stack at the bottom, assigns special registers to store the stack address, and the stack operation has special instructions.

2. Program examples

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

int  b;  
Int a = 0; // Global Initialization Zone
Char * p1; // Global Uninitialized Zone
Main () {int b;// stack
Char s []= "abc"; // stack
Char * P3 = 123456; // 123456/0 is in the constant region, P3 is on the stack.
Static int c = 0; // global (static) initialization zone
p1 =  (char *)malloc(10)
P2 = (char *) malloc (20) /// The area allocated 10 and 20 bytes is in the heap area.
Strcpy (p1, "123456"); and // 123456/0 in the constant region, the compiler may optimize it to a place with the "123456" pointed to by p3.


The difference between stack and stack can be seen in the following metaphor:

Using a stack is like going to a restaurant for dinner, just ordering (application), paying, and eating (use), leaving as soon as you’re full, without paying attention to the preparation of cutting and washing dishes, and cleaning dishes and pots, which has the advantage of being fast, but with less freedom. The use of heap is like doing the dishes that you like to cook, but it is more troublesome, but it is more suitable for your taste, and it has a large degree of freedom. (Classics! )


The above is the whole content of this article. I hope that the content of this article will have some reference value for everyone’s study or work. Thank you for your support for developpaer. If you want to know more about it, please check the links below.