Transferred from: http://www.cnblogs.com/chuncn/archive/2011/04/12/2014273.html
The basics: five large memory partitions
Stacks are stores of variables that are allocated by the compiler when needed, and that are automatically clear when not needed. The variables inside are usually local variables, function parameters, and so on.
Heap , which is the memory blocks allocated by new, their release compiler does not go to the tube, by our application to control, generally a new will correspond to a delete. If the programmer does not release it, the operating system will automatically recycle after the program finishes. (New Char; Delete Char;new char2[2]; Delete[] char2;)
The free storage area , which is the memory blocks allocated by malloc, is very similar to the heap, but it ends up living with no.
Global/Static storage , global variables and static variables are allocated in the same piece of memory, in the previous C language, the global variables are divided into initialized and uninitialized, in C + + There is no such distinction, they occupy the same piece of memory area together.
constant Storage , which is a special piece of storage, they are stored in constant, not allowed to modify (of course, you have to pass the improper means can also be modified, and many methods, in the "Const thinking" article, there are 6 ways.)
--------------------------------------
The main differences between heap and Stack are the following points:
1, management mode : For the stack, is automatically managed by the compiler, without our manual control; for the heap, release work is controlled by the programmer and easily generates memory leak.
2, space size : Generally speaking, in the 32-bit system, heap memory can reach 4G of space, from this point of view heap memory is almost no limit. But for the stack, generally there is a certain amount of space, for example, under the VC6, the default stack space is 1M (as if it is not clear). Of course, we can modify:
Open the project and proceed to the following menu: Project->setting->link, select output in category, then set the maximum and commit of the stack in the reserve.
Note: The reserve minimum value of 4byte;commit is reserved in the virtual memory of the page file, it is set to a large size stack will open up a larger value, may increase the memory overhead and startup time.
3, fragmentation problem : For the heap, frequent new/delete is bound to cause memory space discontinuity, resulting in a large number of fragments, so that the program efficiency is reduced. For the stack, there is no problem, because the stack is advanced out of the queue, they are so one by one correspondence, so that there will never be a memory block from the middle of the stack pop-up, before he pops up, in the back of his upper stack content has been ejected, detailed can refer to the data structure, here we are no longer one by one discussed.
4, growth Direction : For the heap, the growth direction is upward, that is, to the memory address of the direction of increase, for the stack, its growth direction is downward, is toward the memory address of the direction of growth.
5, allocation method : are dynamically allocated, there is no static allocation of the heap. Stacks are allocated in 2 ways: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is assigned by the ALLOCA function (which is not advocated), but the dynamic allocation of the stack is different from the heap, and his dynamic allocation is released by the compiler without our manual implementation.
6, allocation efficiency : The stack is the machine system provides data structure, the computer will support the stack at the bottom: the allocation of dedicated register storage stack address, the stack stack out of a special command execution, which determines the efficiency of the stack is higher. The heap is provided by C + + function library, its mechanism is very complex, for example, in order to allocate a piece of memory, the library function will follow a certain algorithm (the specific algorithm can refer to the data structure/operating system) in the heap memory to search for available enough space, if there is not enough space (possibly due to too much memory fragmentation), It is possible to invoke the system function to increase the memory space of the program data segment, so that there is a chance to divide the memory in sufficient size and then return. Obviously, the heap is much less efficient than the stack.
Heap and Stack, due to the use of a large number of new/delete, prone to large amounts of memory fragmentation, due to lack of dedicated system support, inefficient, due to the possibility of triggering user-state and nuclear mentality of the switch, memory applications, the cost becomes more expensive. So the stack is the most widely used in the program, even if the call of the function is done by the stack, the parameters in the function call, the return address, the EBP and the local variables are all stored in a stack. So, we recommend that you try to use stacks instead of heaps.
Although the stack has so many advantages, it is not so flexible compared to the heap, sometimes allocating a lot of memory space, or heap better.
Whether it is a heap or stack, to prevent the occurrence of cross-border phenomenon (unless you deliberately make it out of bounds), because the result of the cross-border is either a program crash, or destroy the program heap, stack structure, produce unexpected results, even in the course of your program, did not occur above the problem, you still have to be careful, When it's time to blow up, it's pretty hard to debug.
--------------------------------------
Note: exe cannot release memory in DLL
The allocation and deallocation of memory is not done by the same heap management program. The heap in a dynamic-link library is managed by default by the heap manager in Msvcrt.dll (dynamically linked), and the EXE program's heap is by default managed by the program's own code (statically linked), because their heap managers differ when the dynamic-link library allocates memory in EXE, you will get an error when you release it because the heap where the EXE is located does not allocate this memory, and you ask it to release the memory.
The problem is resolved by linking all modules in the program to the C + + run-time library multithreaded DLL, and all operations that allocate and free memory on the heap are managed by the same heap management program.
Go: Memory Release knowledge Summary for C + + +