What is the difference between stack and stack?
The main differences are as follows:
1. Different management methods;
2. Different space sizes;
3. Whether fragments can be generated is different;
4. Different Growth directions;
5. Different allocation methods;
6. Different Allocation Efficiency;
Management Method: For stacks, it is automatically managed by the compiler, without manual control. For stacks, the release work is controlled by programmers and memory leak is easily generated.
Space size: In a 32-bit system, the heap memory can reach 4 GB. From this perspective, there is almost no limit on the heap memory. But for the stack, there is usually a certain amount of space. For example, under vc6, the default stack space is 1 MB (as if so, I cannot remember ). Of course, we can modify:
Open the project and choose Project> setting> link, select output from category, and set the maximum value and commit of the stack in reserve.
Note: The minimum reserve value is 4 byte. Commit is retained in the page file of the virtual memory. Compared with the general setting, commit makes the stack open up a large value, memory overhead and startup time may be increased.
Fragmentation Problems: For the heap, frequent New/delete operations will inevitably lead to discontinuous memory space, resulting in a large number of fragments and reduced program efficiency. For the stack, this problem will not exist, because the stack is an advanced and outgoing queue. They are so one-to-one correspondence that it is impossible to have a memory block popped up from the middle of the stack, before it is popped up, the stack content that comes in after it has been popped up. For details, refer to the data structure. Here we will not discuss it one by one.
Growth direction: For the heap, the growth direction is upward, that is, the direction to the memory address increase; For the stack, the growth direction is downward, is to increase towards memory address reduction.
Allocation Method: The heap is dynamically allocated without static allocation. There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is completed by the compiler, such as local variable allocation. Dynamic Allocation is implemented by the alloca function, but the dynamic allocation of stacks is different from that of stacks. The dynamic allocation of stacks is released by the compiler and does not need to be implemented manually.
Allocation EfficiencyThe stack is the data structure provided by the machine system. The computer will provide support for the stack at the underlying layer: assign a special register to store the stack address, and pressure the stack to output the stack with dedicated command execution, this determines that the stack efficiency is relatively high (My notes:For details about the EBP register, refer to another article ).
The heap is provided by the C/C ++ function library, and its mechanism is very complicated. For example, to allocate a piece of memory, library functions search for available space in heap memory based on certain algorithms (for specific algorithms, refer to data structures/operating systems, if there is not enough space (probably because there are too many memory fragments), it is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, then return. Obviously, the heap efficiency is much lower than the stack efficiency.
From this point, we can see that compared with the stack, the use of a large number of new/delete operations may easily cause a large amount of memory fragments; because of the absence of dedicated system support, the efficiency is very low; because it may lead to switching between the user State and the core state, the memory application will become more expensive. Therefore, stacks are the most widely used in applications. Even function calls are completed using stacks. The parameters and return addresses in the function call process are as follows, both EBP and local variables are stored in stacks. Therefore, we recommend that you use stacks instead of stacks.
Although the stack has so many advantages, but because it is not so flexible as the heap, sometimes it is better to allocate a large amount of memory space.
Whether it is a heap or a stack, it is necessary to prevent cross-border phenomena (unless you intentionally cross-border it), because the cross-border result is either a program crash, either it is to destroy the heap and stack structure of the program to produce unexpected results. Even if the above problem does not occur during your program running, you should be careful, maybe it will collapse at any time, at that time, debug was quite difficult :)