Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the heaps and stacks in C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the heap and stack in C++". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought. Let's study and learn what the heap and stack in C++ are respectively.

As an upgraded version of the C language, C++ has a very powerful function. It can not only support a variety of programming styles, but also has all the functions of C language. What we introduce to you here is one of the more important contents, the basic introduction of the memory region of C++.

C++ memory area is divided into 5 areas. They are heap, stack, free storage, global / static storage, and constant storage.

Stack: a variable store allocated by the compiler when needed and automatically cleared when it is not needed. There are usually local variables, function parameters, and so on.

Heap: a block of memory allocated by new, which is not controlled by the release compiler but controlled by our application, usually a new corresponds to a delete. If the programmer does not release it, the operating system will automatically recycle it after the program ends.

Free storage: a block of memory allocated by malloc, etc., very similar to a heap, except that it uses free to end its own life.

Global / static storage: global and static variables are assigned to the same block of memory, in the previous c language. Global variables are divided into initialized and uninitialized. There is no such distinction in C++. They occupy the same memory together.

Constant storage area: this is a special storage area, which stores constants and is not allowed to be modified.

The difference between heap and stack in C++ memory area:

Different management methods: the stack is automatically managed by the compiler without our manual control; for the heap, the release is done by the programmer, which is prone to memory leaks.

The size of the space is different: generally speaking, under the 32-for system, the heap memory can reach 4G of space, from this point of view, there is almost no limit to the heap memory. But for stacks, there is usually a certain amount of space. For example, under vc6, the default stack size seems to be 1m. Of course, you can also modify it yourself: open the project. Project-- > setting-- > link, select output in category, and then set the maximum value and commit of the stack in reserve.

Whether fragmentation can be generated: for the heap, frequent new/delete will inevitably cause discontinuity of memory space, resulting in a large number of fragments and reducing the efficiency of the program. As far as the stack is concerned, this problem does not exist.

The growth direction is different: for the heap, the growth direction is upward, that is, towards the direction of increasing the memory address; for the stack, its growth mode is downward and toward the direction of decreasing memory address.

The allocation mode is different: the heap is allocated dynamically; the stack has two allocation modes: static and dynamic. Static allocation is done by the compiler, such as the allocation of local variables. Dynamic allocation is done by the alloca function, but the dynamic allocation of the stack is different from the heap, and its dynamic allocation is released by the compiler without our manual implementation.

The allocation efficiency is different: the stack is the data structure provided by the machine system, and the computer will provide support to the stack at the bottom: allocating special registers to store the stack address, pressing the stack out of the stack has special instructions to execute, which determines the efficiency of the stack is relatively high. The heap is provided by the cmax cmarker + library function, and the mechanism is very complex. Library functions are assigned according to a certain algorithm. Obviously, the efficiency of the heap is much lower than the stack.

Images in process memory, mainly code area, heap (dynamic storage area, new/delete dynamic data), stack, static storage area

Direction of memory area address from low to high: code area, static storage area, heap, stack

Heap "and" stack "are independent concepts. Generally speaking," stack "is actually two concepts:" heap "and" stack ". In English, heap is heap, stack is stack, I don't know when and why, in Chinese, these two different concepts have been mixed up, so the misunderstandings and disputes around this mixed word have not been broken in recent years.

The stack is generally implemented by hardware (CPU). CPU uses the stack to hold the return address when calling subroutines (functions), and high-level languages sometimes use it as a storage space for local variables.

Heap is a real software concept, and it is entirely up to the programmer to explicitly it or not, such as malloc.

After the program is compiled and connected to generate the execution program, the starting address of the stack and stack has been determined (specifically, through the "linker"). On a CPU with a reverse growth stack, the data space can be expressed as follows:

Low-> |-|

| | Global quantity (all initialized quantities .data, | |

| | Uninitialized quantity .bss) |

Heap start-> |-|

| | increase the heap to a high address |

| | |

| | |

| | Free space |

| | |

| | |

| | Stack grows to a low address |

High Stack start-> |-|

In memory, "heap" and "stack" share all the free space, but their starting addresses and growth directions are different, and there is no fixed boundary between them. If at run time, the "heap" and "stack" grow to cover each other, called "stack conflict", the system is bound to collapse. Due to overhead reasons, all kinds of compilers do not consider to solve this problem in the implementation, only by the designer themselves, such as increasing memory and so on.

=

Description (128 as an example) hard stack:

That is, SP, the so-called stack (used for PC pointer isobaric stack) in assembly is generally set to grow downwards from the top 0X10FF of the on-chip RAM. Basically, 64 is enough.

Software stack:

The stack automatically allocated by the C compiler, the space between the hard stack and global variables, also grows downward and is generally used for local variables. For example, if a subroutine defines a local variable A [256], then this space is in the soft stack. Suppose the current soft stack uses 0X800, and after assigning A [256], the soft stack uses 0X700jue A [0] address 0X700jue A [1] address is 0X701. Of course, if there are few local variables, you can use registers instead of soft stacks. The soft stack returns to 0X800 after this subroutine exits.

Another: after your C program is compiled, R28:R29 is the soft stack pointer in the assembly file generated.

Thank you for your reading. The above is the content of "what are the heap and stack in C++?" after the study of this article, I believe you have a deeper understanding of what the heap and stack in C++ are. The specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report