Stack and stack differentiate between growth direction and storage direction

Source: Internet
Author: User

C + + as an upgraded version of the language, has a very powerful function. It not only can support various programming styles, but also has all the functions of C language. We are here to introduce you to one of the more important content, C + + memory area of the basic introduction.

The C + + memory area is divided into 5 regions. Are heap, stack, free storage area, global/static storage and constant storage.

stack: A variable store that is allocated by the compiler when it is needed and automatically purged when it is not needed. There are usually local variables, function parameters, and so on.

heap: blocks of memory allocated by new, their release compiler does not go to the tube, by our application to control, generally a new corresponding to a delete. If the programmer is not released, the operating system will automatically recycle after the program finishes.

free storage: a chunk of memory allocated by malloc, which is very similar to a heap, but uses freedom to end its own life.

Global/Static storage: global variables and static variables are assigned to the same memory in the previous C language. Global variables are both initialized and uninitialized, and there is no such distinction in C + + that they share the same piece of memory.

constant Store: This is a special storage area where constants are stored, and modifications are not allowed.

differences in heap and stack in C + + memory areas:

Different management methods : Stack is managed by the compiler, without our manual control, for the heap, release by the programmer, easy to generate memory leaks.

different size: Generally speaking, under 32 for the 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 size seems to be 1M. Of course, can also modify their own: Open the project. Project-->setting-->link, select output in category, and then set the maximum stack value and commit in the reserve.

can produce fragmentation: 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. This problem does not exist for the stack.

The growth direction is different: for the heap, the growth direction is upward, that is, toward the memory address of the direction of increase, for the stack, it is the way to grow downward, is to the memory address to reduce the direction of growth.

different distribution methods: The heap is dynamically allocated, and the stack has both static and dynamic allocation methods. Static allocations are done by the compiler, such as the allocation of local variables. Dynamic allocation is performed by the Alloca function, but the dynamic allocation and heap of the stack are different, and its dynamic allocation is released by the compiler without our manual implementation.

distribution efficiency is different: the stack is the 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 C/s library functions, and the mechanism is complex. Library functions are assigned according to a certain algorithm. Obviously, the heap is much less efficient than the stack.

Image in process memory, mainly code area, heap (dynamic storage area, New/delete Dynamic Data), stack, static storage area

Memory area address from low to high direction: code area, static storage area, heap, stack

Heap "and" stack "are independent concepts. The usual" stack "is actually two concepts:" Heap "and" stack ". In English, the heap is heap, stack is stack, do not know when, what reason, in Chinese, these two different concepts just get together, so, around this mixed word misunderstanding and disputes in these years have not broken. The  

Stack is typically implemented by hardware (CPU), where the CPU uses the stack to hold the return address of the calling subroutine (function), and the advanced language sometimes uses it as a local variable storage space. &NBSP

Heap is a real software concept, and it is entirely up to the programmer to "show (explicitly)", such as malloc. &NBSP

After the program has been compiled to build the execution program, the starting address of the heap and stack has been determined (specifically, through "Connecting programs"), on a CPU with a stack with a reverse growth, the data space can be represented as follows:  

Low->   |-----------------|&NBSP
| Global amount (all initialized  .data, | 
| uninitialized  .bss ) | 
                 Heap Start->|-----------------| 
| Heap to high address growth | 
| | 
|                 |&NBSP
| free space | 
| | 
| |&NBSP
| Stack to low address growth | 
High stack start->|-----------------| 


In memory, "heap" and "stack" share all the free space, except that their starting address and growth direction are different, they do not have a fixed boundary between, if at runtime, "Heap" and "stack" grow to overlap, known as "stack heap conflict", the system will certainly collapse. Due to cost reasons, all kinds of compilation in the implementation has not considered to solve the problem, only by the designers themselves, such as increasing memory. &NBSP

================================================================= 
Description (128 for example) hard stack: 
that is, the SP, commonly referred to in the assembly as the so-called stack (for PC pointers, etc.), the general settings from the top of the ram 0x10ff start downward growth, basically 64 enough  
Software stack:  
C compiler automatically allocated stacks, The space between the hard stack and the global variable is also downward-growing, generally for local variables. such as a subroutine to define a local variable a[256], then this space is in the soft stack, assuming that the current soft stack is used to 0x800, assign a[256] after the soft stack to 0x700,a[0] address 0x700,a[1] address for 0x701  Of course, if there are fewer local variables, you can use a register, no soft stack. The soft stack reverts to 0x800 after this subroutine exits. &NBSP
Another: After compiling your C program, the r28:r29 is the soft stack pointer  

A generic hard stack is set automatically in the compilation options and the soft stack compiler. You just have to see if the *.mp file is reasonable.

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.