First, Memory layout
1, Stack: The compiler automatically allocates the release, storing the function parameter value, local variable value, etc., its operation method is similar to the stack in the data structure.
2, heap area (heap): Generally by the programmer allocation release, and the data structure in the heap has nothing to do with the distribution pattern similar to the list.
3. Global/static zone (Static): The storage of global variables and static variables is put together and allocated at program compile time.
4, the literal constant area: To store the constant string.
5. Program code area: binary code that holds the function body (member functions of the class, global functions)
The comparison between stack and heap
1. Application method
Stack: System is automatically assigned, such as declaring int A; The system automatically opens up space for a in the stack space
Heap: The programmer applies and indicates the size of malloc in C, such as char*p= (char*) malloc (10);
The new operator in C + +: such as int*p2=new Int (10);
Note: P and P2 itself are in the stack, but the address they point to is the heap space
2. System response
Stack: As long as the system remaining space is larger than the application space can apply, or error: Stack Overflow
Heap: First of all should know that the operating system has a record of the free memory address of the list, 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 in this memory space is recorded at the first address so that the DELETE statement in the code can properly free up the memory space. Also, because the size of the found heap node does not necessarily equal the size of the request, the system automatically re-places the extra portion into the idle list.
3, the application size limit
Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.
Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. Thus, the space obtained by the heap is limited by the effective virtual memory in the computer system, which is more flexible and larger.
4. Application efficiency
The stack is automatically allocated by the system faster, the heap is allocated slowly by new, and is prone to memory fragmentation, but easy to use.
C + + Program Memory allocation method (heap and stack)