In C + +, memory is divided into 5 areas, they are the heap, stack, free storage area, global/static storage and constant storage area.
Stacks are those that are allocated by the compiler when they are needed, and are stored automatically when they are not needed. The variables inside are usually local variables, function parameters, and so on.
Heap, that is, those allocated by the new memory block, 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 is not released, the operating system will automatically recycle after the program finishes.
Free storage, which is the chunk of memory allocated by malloc, he and the heap are very similar, but it is to use free to end their own lives.
Global/static storage, global variables and static variables are assigned to the same memory, in the previous C language, the global variables are divided into initialized and uninitialized, in C + + without this distinction, they share the same memory area.
Constant store, this is a special storage area, they are stored in a constant, not allowed to modify (of course, you have to use improper means can also be modified, and many methods, in the "Const thinking" article, I gave 6 methods)
Explicitly differentiate between stacks and stack
On the BBS, heap and stack of the distinction between the problem, seems to be an eternal topic, it can be seen that beginners are often confused, so I decided to take his first surgery.
First, let's give an example:
void F () {int* p=new int[5];}
This short sentence contains the heap and stack, see new, we should first think, we allocate a heap of memory, then the pointer p. He allocates a stack of memory, so this means that a pointer to a heap of memory is stored in the stack memory. When the program determines the size of the memory allocated in the heap, then calls operator new to allocate memory, then returns the first address of the memory, put it into the stack, and his assembly code under VC6 is as follows:
00401028 Push 14h
0040102A call operator new (00401060)
0040102F Add esp,4
00401032 mov dword ptr [Ebp-8],eax
00401035 mov eax,dword ptr [ebp-8]
00401038 mov dword ptr [Ebp-4],eax
Here, we do not release the memory for simplicity, so how to release it. Is it delete p? O, wrong, should be delete []p, this is to tell the compiler: I delete an array, VC6 will be based on the appropriate cookie information to release the memory work.
Well, let's go back to our topic: What's the difference between stacks and stacks?
The main difference consists of the following points:
1. Different ways of management;
2, the space size is different;
3, can produce different fragments;
4, the growth direction is different;
5. Different ways of distribution;
6, the distribution efficiency is different;
Management mode: For the stack, is the compiler automatically managed, without our manual control; for the heap, the release of the work by the programmer control, easy to produce memory leak.
Space size: Generally in 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 size is 1M (as if it is not clear). Of course, we can modify:
Open the project, and then operate the menu as follows: Project->setting->link, select output in category, and then set the maximum stack value and commit in the reserve.
Note: The reserve minimum value is 4byte;commit is kept in the virtual memory of the page file, it set a large stack to open up a larger value, may increase the cost of memory and startup time.
Fragmentation problem: For the heap, frequent new/delete is bound to cause the memory space discontinuity, resulting in a large number of fragments, so that the program efficiency is reduced. For stacks, this problem does not exist because the stack is an advanced queue, they are so one by one corresponding, so that there will never be a memory block from the middle of the stack pop-up, before he pops up, the last of the stack content on his top has been ejected, detailed reference to the data structure, here we will no longer one by one discussion.
Growth direction: For the heap, the growth direction is upward, that is, toward the memory address of the direction of increase, for the stack, its growth direction is downward, is to the memory address to reduce the direction of growth.
Allocation method: The heap is dynamically allocated and there is no statically allocated heap. There are 2 ways to allocate the stack: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is allocated by the Alloca function, but the dynamic allocation and heap of the stack are different, and his dynamic allocation is released by the compiler without our manual implementation.
Allocation efficiency: The stack is a machine system provides the data structure, the computer will be at the bottom of the stack to provide support: the allocation of special registers to store the stack address, pressure stack out of the stack have specific instructions to execute, which determines the stack of high efficiency. The heap is provided by the C + + function library, and its mechanism is very complex, for example, in order to allocate a piece of memory, the library function will search the heap memory for the available space of sufficient size in a certain algorithm (the specific algorithm can refer to the data structure/operating system), if there is not enough space (possibly due to too much memory fragmentation), 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 get enough memory and then return. Obviously, the heap is much less efficient than the stack.
From here we can see that heap and stack, because of the use of a large number of new/delete, easy to create a lot of memory fragmentation, because there is no special system support, inefficient, because of the possible user-state and nuclear mentality of switching, memory applications, the cost becomes more expensive. So the stack in the program is the most widely used, even if the function of the call also use stack to complete, function calls in the process of parameters, return address, EBP and local variables are used to store the stack. So, we recommend that you try to use stacks instead of heaps.
Although the stacks have so many benefits, they are not as flexible as the heap, and sometimes allocate a lot of memory space, or use a heap better.
Both the heap and the stack are supposed to prevent the occurrence of cross-border phenomena (unless you deliberately make them out of bounds), because the result of crossing the line is either a program crash or a heap or stack structure that destroys the program, resulting in unexpected results, even when your program is running without the above problems, you should be careful Maybe it will collapse when debug is very difficult:
2,
Heap and stack differences (paste)
Not myself! Because it is very classic, so nationalized, and the public read it! The original author is ominous! The difference between heap and stack
I. Preliminary knowledge-memory allocation for programs
a program compiled by C + + + + uses memory that is divided into the following sections
1, stack area (stack)- The compiler automatically allocates and releases the , and holds the function's parameter value, the local variable value, and so on. The operation is similar to the stack in the data structure.
2, Heap area (heap) - is typically assigned by programmers to release, 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, the distribution is similar to the list, hehe.
3, Global Zone (Static)--the storage of global and static variables is put together, initialization global and static variables in one area, uninitialized global variables and uninitialized static variables in another contiguous area.) - after the program has been released system release
4, literal constant area-the constant string is here. After the program is finished, the system releases the
5, program code area-the binary code that holds the function body.
Second, example program
This is written by a predecessor, very detailed
//main.cpp
int a = 0; Global initialization area
char *p1; Global uninitialized Zone
Main ()
{
int b; stack
char s[] = "abc"; stack
char *p2; stack
char *p3 = "123456"; 123456\0 in the constant area, p