Stack and stack differences
1. Application Method
(1) stack (satck): automatically allocated by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.
(2) heap: the programmer must apply for it (call malloc, realloc, calloc), specify the size, and release it by the programmer. Memory leak is easy to generate.
Eg: char p;
P = (char *) malloc (sizeof (char ));
However, p itself is in the stack.
2. Application size limit
(1) STACK: in windows, the stack is a data structure extended to the base address and a continuous memory area (its growth direction is opposite to that of the memory ). The stack size is fixed. If the requested space exceeds the remaining space of the stack, overflow is displayed.
(2) Heap: the heap is a high-address extended data structure (which has the same growth direction as the memory) and is a discontinuous memory area. This is because the system uses a linked list to store idle memory addresses, which are naturally discontinuous, And the traversal direction of the linked list is from the base address to the high address. The heap size is limited by the valid virtual memory in the computer system.
3. system response:
(1) STACK: as long as the stack space exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
(2) Heap: First, you should know that the operating system has a linked list that records the idle memory address, but when the system receives the application from the program, it will traverse the linked list, find the heap node with the first space greater than the requested space, delete the node from the idle linked list, and allocate the space of the node to the program. In addition, for most systems, the size of the allocation will be recorded at the first address in the memory space, so that the free statement in the code can correctly release the memory space. In addition, the size of the heap node is not necessarily equal to the applied size. The system automatically places the excess part in the idle linked list.
NOTE: 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. This problem does not exist for stacks,
4. Application Efficiency
(1) the stack is automatically allocated by the system, which is fast. But programmers cannot control it.
(2) The heap is the memory allocated by malloc, which is generally slow and prone to fragmentation, but it is most convenient to use.
5. Storage content in heap and stack
(1) STACK: When a function is called, the address of the next statement in the first main function to be added to the stack, and then the parameters of the function, the parameter is from right to left and then the local variable in the function. Note: static variables are not included in the stack.
When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function, where the program continues to run.
(2) Heap: Generally, the heap size is stored in one byte in the heap header.
6. access efficiency
(1) Heap: char * s1 = "hellow tigerjibo"; it is determined at compilation.
(2) STACK: char s1 [] = "hellow tigerjibo"; assigned values at runtime; faster with arrays than with pointers, in the underlying assembly, the pointer needs to be transferred using the edx register, while the array is read on the stack.
Supplement:
The 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 all the output stacks of the Pressure stack have special command execution, this determines the high efficiency of the stack. 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.
7. allocation method:
(1) The heap is dynamically allocated without static allocation.
(2) 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 performed by the alloca function, but the stack dynamic allocation is different from the heap. Its Dynamic Allocation is released by the compiler without manual implementation.