Kevingao, original address
The memory used by a program compiled by C + + is divided into the following sections:
1. Stack: The compiler automatically assigns the release, the parameter value of the stored function, the value of the local variable, etc., and its operation is similar to the stack of data structure.
2, heap area (heap): Generally by the programmer assigned to release, if the programmer does not release, the program may end up by the OS recycling, it is worth noting that he and the data structure of the heap is two different, distribution is similar to the data structure of the linked list.
3, Global Zone (Static): Also known as static data memory space, storage of global variables and static variables, global variables and static variables are stored in a block, the initialization of global variables and static variables put a piece of area, not initialized in another area adjacent to the end of the program is released by the system.
4, text constant area: the constant string is placed here, the program is released after the end of the system.
5, program code area: the binary code that holds the function body.
The difference between heap and stack:
1, from the above review can be learned that their program memory allocation method is different.
2. Application and response are different:
(1) How to apply:
The stack is automatically assigned by the system, the system is retracted, the heap needs to be applied by the programmer himself, C is allocated with the function malloc, the free is released, C + + is allocated with new, and the delete is released.
(2) Response of the system after application:
Stack: As long as the remaining space on the stack is larger than the requested space, the decency will provide memory for the program, otherwise it will report the exception prompt stack overflow.
Heap: The first thing you should know is that the operating system has a list of memory address, when the system receives the application of the program, it will traverse the list, look for the first space is larger than the requested space of the heap node, and then delete the node from the list of idle nodes, and the node's space allocated to the program. Also, for most systems, the size of this allocation is recorded at the first address in the memory space, so that the delete or free statements in the code can properly release the memory space. Also, because the size of the found heap node does not necessarily equal the size of the request, the system will re-place the extra portion into the idle list.
3, the size of the application of different restrictions:
Stack: Under Windows, the stack is the data structure to the low address extension, is a contiguous memory area, the top of the stack address and the maximum capacity of the stack is the system pre-defined, can be obtained from the stack less space.
Heap: The heap is the data structure to the high address extension, is a discontinuous memory area, this is because the system is stored by the list of free memory address, the natural heap is a discontinuous memory area, and the chain list traversal is from the low address to high address traversal, heap size is limited by the computer system's effective virtual memory space, thus space, The space for the heap is more flexible and larger.
4, the efficiency of the application is different:
Stack: The stack is automatically assigned by the system and is fast, but the programmer can't control it.
Heap: Heap is a programmer's own allocation, slow, easy to produce debris, but easy to use.
5, heap and stack storage content is different:
Stack: When a function is called, the first stack is the main function after the function call the address of the next instruction, and then the parameters of the function, in most of the C compiler, the argument is from right to left into the stack, when the function call ends, the local variable first out of the stack, then the parameter, the last stack pointer to the first saved address, The next instruction in the main function.
Heap: Usually in the heap of the head with a byte to hold the heap size, specific content by the programmer to arrange
[Go] stack space and heap space