An analysis of the differences between C++ using pointer stacks and stacks

  • 2020-04-01 21:35:17
  • OfStack

Data can be stored in memory in the following forms

1. Stack area -- it is automatically allocated and released by the compiler. This area usually holds the parameter values of functions, values of local variables, etc.
2. Heap area - usually allocated by the programmer to release, if the programmer does not release, the program will be recovered by the operating system at the end of the time,
3. Register area -- used to store stack top pointer and instruction pointer
4. Global to -- is also a static area, global variables and static variables are stored together, the initialized global variables and static variables are stored in the same area, for the initialized global variables and static variables in another adjacent area, after the end of the program by the system release.
5. Literal constant area -- this is where the constant string is placed and is released by the system after the program is finished.
6. Program code area -- contains the binary code of functions.

Function parameters and local variables are stored in the stack, and their memory space is freed when the function runs or returns to the system, but global variables are not freed by the system. Global variables are released by the system only after the program is finished, and since global variables are Shared by all class members and functions, they can be easily modified to solve this problem
We're going to use the heap

Stack difference

1. Different application methods

The heap is applied by the programmer himself
The stack is automatically assigned by a system of programmed local variables or functions

2. The response of the system is different

Stack - as long as the stack is larger than the requested space, the system will provide memory for the program, otherwise the stack will be prompted to overflow

Heap - system control after receipt of the application, will traverse an operating system used to address memory control records list, when it finds a heap space is greater than the applied control node will be after the node is removed from the record memory free address list, and the nodes of the memory allocated to the program, and then in the area of the size of the first address allocation,         In this way, when we use delete to free memory, delete can correctly identify and delete all variables in this memory area. In addition, the memory space we apply for may not be equal to the memory space on the heap node. At this time, the system will automatically recycle the extra memory space on the heap node to the free linked list

3. The size of the space is different

- in the condition of Windows, stack is a contiguous memory area, its size is 2 m, some said 1 m, all in all this value is set is a compile time constant, is by the system according to the address on the top of the stack and the stack in advance the capacity of the defined, join your data application memory space than the stack space, will prompt overflow, so don't expect the stack can store larger data.

Heap - heap is a discontinuous memory area, each block area by a linked list to string them, the memory space is called the heap, its size is determined by the virtual memory in the system, so the space is larger, and the way to get space is also more flexible.

4. Difference in execution efficiency

Stack - stacks are allocated automatically by the system, so they are faster, but the programmer cannot manipulate them.
Heap - heap is the memory allocated by the programmer.       Generally slow and prone to memory fragmentation, but very convenient to use.

5. Different execution functions

Stack - when a function is called, the first thing on the stack is the memory address of the next line of the called function, followed by the function's arguments, if there is more than one argument, the order is from left to right, and finally the function's local variables.

Due to the stack after advanced principle, on the contrary, at the end of the function at first in first out stack for local variables, and then the parameters of the order from left to right, then all variables are out of the stack, first into the line of the stack pointer natural memory address, which is called function to the next line of memory address, program according to the address call functions to jump to the next line of automated.

Because of the stack's first-come-first-out principle, it is never possible to generate memory fragmentation

Pile, pile is a pile of discontinuous area of memory, in the system by the list them together, so at the time of use must be arranged by the programmer, his mechanism is very complicated, sometimes in order to assign a suitable piece of memory, programmers need to according to certain algorithm search in the heap memory has enough space available, if not meet the conditions of space, it will have to send application to memory part of memory space, so as to have the opportunity to allocate enough memory, and then will return to that calculated values obviously heap, much lower than the stack operation efficiency, and easy to produce pieces, But the advantage is that the heap can store fairly large amounts of data, and some of the details can be arranged by the programmer.

So those are the stack differences but the trade-offs in the application are still very much a case by case basis


Related articles: