Where the memory partition should be noted

Source: Internet
Author: User
Tags stack pop
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 hold a constant, not allowed to modify (of course, you have to use improper means can also be modified, and many 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:
Yes, there is another thing, if someone put the stack together said, it means that the stack, is not a heap, hehe, clear.

The "heap" of English is heap; "Stack" = "stack", English is stack.
We avoid the meaning of computer, only to see the daily use of righteousness. Check "The Little Oxford Dictionary" (equivalent to the "Xinhua Dictionary" looks), there are:
Heap: [noun] disorderly pile; (esp. in plural) [Colluquial] large number or amount; ...
stack: [noun] (esp. orderly) pile or heap; Haystack ...
It can be seen that two are heap, stack meaning, the difference is that heap is disorderly, and stack is orderly. For example, if it is a stack of cards, did not wash the disorderly place is heap, wash the neatly placed is stack. Because two words are meant to be relatively close, there are also common places.

On a computer, heaps and stacks generally refer to two types of data structures. The "Heap" is a pyramid-shaped structure that requires more than the value below (or vice versa) and is less orderly; "Stack" is a linear structure with a strict sequence of entry and exit. This is the intuitive explanation of its difference.

Talk about the question of Chinese.
The translation of heap into "heap" by a Chinese translator is a literal translation, and the stack as a "stack" is entirely another metaphor in Chinese.
The so-called "stack", that is, "Plank Road", is built on the mountain wall a very narrow way, one can only allow one person to pass. The order of the "stack" structure in the computer is not arbitrary.
As for the "stack", I think it is completely a patchwork of things, but let people literally do not see a point of the original meaning.


Add two sentences
The stack is basically the 2 different regions in this plate that address memory
But they have another meaning in the data structure:
Stack: A type of linear table that only allows the operation of the last element (called the stack top) to add an element to the top of the stack to form a new stack (push) or to remove the original stack from "exposing" a new element as the top of the stack (POP).
Heap: A sort of binary tree structure, with a large top heap and a small top heap, a typical application is a heap sort (there is no connection to what we often say about the heap memory area).

C + + Five memory areas upstairs already someone has spoken. Stack and heap two areas are divided because they are managed differently

The stack is used to save the local variable of the function it is the same thing as the stack segment of assembly language. Its operation is similar to the stack in the data structure, hence the name. But it can also use the stack offset pointer for random access in addition to push and pop operations using the top and stack pointers, so the top pointer is used only when the function is called and when the function returns. The stack is highly efficient because the object on which it is destroyed is only a pointer saved in the mobile register. However, the total length of the general stack segment is limited, too long recursion may lead to stack overflow errors (very common bar ^)

Heap is released when malloc request is assigned to free specifies that it is not automatically released is one of the main causes of memory leaks and that its idle and usage is managed by the operating system with a linked list so generally speaking, the heap is inefficient, although the language standard doesn't make any hints. It is generally assumed that the heap is the same area as free storage (as if I looked at the same data as before) exceptional C + + is Malloc/free allocated free storage is new/delete allocated)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.