Basic knowledge:
A stack is a simple data structure, a linear table that is only allowed to be inserted or deleted at one end. The one end of the Allow insert or delete operation is called the top of the stack, and the other end is called the bottom, and the insert and delete operations on the stack are called into the stack and out of the stack. There is a set of CPU instructions that can implement stack access to the memory of the process. wherein, the pop instruction implements the stack operation, and the push instruction implements the stack operation. The CPU's ESP register (stack pointer, Pointer) holds the stack-top pointer of the current thread, and the EBP register (base Pointer) holds the current thread's stack-bottom pointer. (both ESP and EBP are called pointer registers pointerregister) The EIP register of the CPU (instruction pointer register instructionpointer) holds the memory address of the next CPU instruction, and when the CPU finishes executing the current instruction, Reads the memory address of the next instruction from the EIP register, and then resumes execution.
1, memory allocation aspects:
Heap: Typically released by programmers, if the programmer does not release, the program may end up being recycled by the OS. Note that it is different from the heap in the data structure, and the distribution is similar to a linked list. The following keywords may be used: New, malloc, delete, free, and so on.
Stack: The compiler (Compiler) automatically allocates the release, storing the function's parameter value, the value of the local variable, and so on. It operates in a manner similar to a stack in a data structure.
2. Application Method:
Heap: Requires programmers to apply themselves and indicate size. In C, the malloc function such as P1 = (char *) malloc (10); Use the new operator in C + +, but note that P1, p2 itself is in the stack. Because they can still be considered as local variables.
Stack: Automatically assigned by the system. For example, declare a local variable int b in the function, and the system automatically opens up space for B in the stack.
3. System response:
Heap: The operating system has a linked list that records the free memory address, when the system receives the application, it iterates through the list, finds the first heap node that is larger than the requested space, and then removes the node from the list of idle nodes and assigns the node's space to the program, in addition, for most systems, The size of this allocation is recorded at the first address in this memory space, so that the DELETE statement in the code can properly free up the memory space. In addition, because the size of the found heap node does not necessarily equal the size of the application, the system will automatically re-put the extra portion into the idle list.
Stack: As long as the remaining space of the stack is larger than the requested space, the system will provide memory for the program, otherwise it will report the exception prompt stack overflow.
4. Size limitation:
Heap: Is the data structure that extends to the high address, which is a discontinuous memory area. 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. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.
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 address and the maximum capacity of the stack is the system pre-defined, under Windows, the size of the stack is fixed (is a compile-time determination of the constant), if the requested space exceeds the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.
Update: Carefully review the relevant information, get the following a method to determine the size of the stack, my Win7 32 bits, the size of the stack default to 1M, the size of the stack is each program is not the same, can be set, in VS2010, Project-Project Properties-Configuration Properties-linker-system-Stack reserve size You can set the stack size of the program directly.
5, Efficiency aspects:
Heap: Is the memory allocated by new, generally slow, and easy to produce memory fragmentation, but the most convenient to use, in addition, in Windows, the best way is to use VirtualAlloc to allocate memory, he is not in the heap, nor is the stack is directly in the process of the address space to retain a fast memory, Although it is most inconvenient to use. But the speed is fast, also the most flexible.
Stack: Automatically allocated by the system, faster. But programmers can't control it.
6, storage content aspects:
Heap: The size of a heap is typically stored in a heap at the head of a pile. The concrete contents of the heap are arranged by programmers.
Stack: The first stack in a function call is the address of the next instruction in the main function (the next executable statement of the function call statement) and then the parameters of the function, in most C compilers, the arguments are left-to-right and then local variables in the function. Note: Static variables are not in the stack. When the function call is finished, the local variable is first out of the stack, then the parameter, and the last stack pointer points to the first saved address, which is the next instruction in the main function, and the program continues to run from that point.
7, Access efficiency aspects:
Heap: Char *s1 = "Hellow Word"; it is determined at compile time;
Stack: char s1[] = "Hellow Word"; it is assigned at run time; it is faster to use an array than a pointer, because the pointer needs to be brokered in the underlying assembly with the EDX register, and the array is read directly on the stack.
Update: The analogy of heap and stack:
Use the stack like we go to a restaurant to eat, just order (send application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but the freedom is small. The use of the heap is like a DIY dish that you like to eat, more trouble, but more in line with their own tastes, and great freedom.
Http://www.cnblogs.com/project/archive/2011/08/19/2145214.html
C + + heap and stack comparison (7 differences)