Heap and stack differentiate growth direction

Source: Internet
Author: User
Tags constant manual

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

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

stack: A variable store that is allocated by the compiler when needed and automatically purged when it is not needed. The inside is usually the local variable, function parameter and so on.

Heap: The memory blocks 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 does not release it, the operating system will automatically recycle after the program finishes.

free storage: a chunk of memory allocated by malloc, like a heap, is very similar, though it uses free to end its life.

Global/Static storage: global variables and static variables are assigned to the same block of memory in the previous C language. Global variables are also initialized and uninitialized, and in C + + There is no such distinction, and they collectively occupy the same piece of memory.

constant Store: This is a special storage area, which is a constant, not allowed to modify.

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

Management is different : the stack is automatically managed by the compiler, without our manual control; for the heap, release is done by the programmer and is prone to memory leaks.

The size of the space is different: generally speaking, under 32 is the system, heap memory can reach 4G 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, you can modify it yourself: Open the project. Project-->setting-->link, select output in the category, and then set the stack's maximum value and commit in the reserve.

can produce fragmentation: for the heap, frequent new/delete will inevitably cause the memory space discontinuity, resulting in a large number of fragments, the program efficiency is reduced. For the stack, there is no problem.

growth direction is different: for the heap, the direction of growth is upward, that is, to the memory address of the direction of increase, for the stack, it is growing in a downward direction, is toward the memory address of the growth.

The allocation method is different: 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 of the stack is different from the heap, and its dynamic allocation is freed by the compiler without our manual implementation.

The allocation efficiency is different: the stack is the machine system provides the data structure, the computer will support the stack at the bottom: allocates the special register to hold the stack the address, the pressure stack out the stack has the specialized instruction execution, this determines the stack the efficiency is high. The heap is provided by C/s library functions, and the mechanism is complex. Library functions are allocated according to certain algorithms. Obviously, the heap is much less efficient than the stack.

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

Memory area address from low to high direction: Code area, static store, heap, stack

Heap "and" stack "are independent concepts the usual" stack "is actually two concepts:" Heap "and" stack ". In English, heaps is heap, stack is stack, do not know when, what reason, in Chinese, these two different concepts are just being together, so, the misunderstanding and dispute around this mixed word has not broken in the past few years.

"Stack" is generally implemented by hardware (CPU), the CPU stack to save the Call subroutine (function) when the return address, high-level language is sometimes used as a local variable storage space.

"Heap" is a real software concept, and whether it is used or not is entirely determined by the programmer "explicitly", such as malloc.

After the program has been compiled to generate the execution program, the start address of the heap and stack has been determined (specifically, through "connectors") on a CPU with a reverse-growing stack, the data space can be represented as follows:

Lo->|-----------------|
| Global amount (all initialized amount. data, |
| uninitialized amount. BSS) |
Heap Start->|-----------------|
| Heap to high address growth |
| |
| |
| free space |
| |
| |
| stack to low address growth |
Gao Start->|-----------------|

In memory, "heap" and "stack" share all the free space, but their starting address and growth direction are different, they do not have a fixed limit, if at run time, "heap" and "stack" grow to overlap with each other, known as "stack heap conflict", the system must collapse. Because of the cost of the reasons, all kinds of compilation in the implementation are not considered to solve this problem, only by the designer to solve their own, such as increased memory.

=================================================================
Description (128 for example) hard stack:
That is, SP, usually assembled in the so-called stack (for PC pointers and other stacks), general settings from the top of the on-chip Ram 0X10FF began to grow downward, basically 64 is enough
Software Stacks:
The C compiler automatically allocates stacks, spaces between hard stacks and global variables, and also grows downward, typically for local variables. For example a subroutine defines a local variable a[256], then this space is in the soft stack, assuming that the current soft stack used to 0x800, assign a[256], the soft stack used to 0x700,a[0] address is 0x700,a[1] address is 0x701 ..., of course, if the local variables are small, With registers on it, no soft stacks are available. This subroutine exits after the soft stack is restored to 0x800.
Another: After your C program compiles, the generated assembly file, r28:r29 is the soft stack pointer

The soft stack compiler automatically sets the general hard stack as long as it is set in the compilation options. You just have to see if the *.mp file is reasonable.

Source: http://hi.baidu.com/walker20100000/blog/item/eac9c714e8c615174a90a7f0.html

http://www.ourdev.cn/bbs/bbs_content_all.jsp?bbs_sn=887201

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.