In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how C/C++ memory is managed". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
C/C++ gives programmers the freedom to manage memory, is a C/C++ language features, although this introduces complexity and danger, but on the other hand, it also increases the control and flexibility, is C/C++ unique, but also powerful.
C/C++ Memory Distribution
Let's start with the following code:
int globalVar = 1;static int staticGlobalVar = 1;void Test(){ static int staticVar = 1; int localVar = 1; int num1[10] = { 1, 2, 3, 4 }; char char2[] = "abcd"; char* pChar3 = "abcd"; int* ptr1 = (int*)malloc(sizeof (int)* 4); int* ptr2 = (int*)calloc(4, sizeof(int)); int* ptr3 = (int*)realloc(ptr2, sizeof(int)* 4); free(ptr1); free(ptr3);}
Do you know where each part of the code is stored in memory?
[Description]
Stack is also called stack, which is used to store non-static local variables/function parameters/return values, etc. Stack is downward growth.
Memory mapping segment is an efficient I/O mapping method for loading a shared dynamic memory library. Users can use the system interface to create shared memory for interprocess communication.
3. Heap is used to store runtime dynamic memory allocation, heap is upward growth.
4, the data segment is also called static area, used to store global data and static data.
Code segments, also known as constant areas, are used to store executable code and read-only constants.
By the way: why is stack growing downward and heap growing upward?
Simply put, in general, in the stack area to open up space, the first to open up the space address is higher, and in the heap area to open up space, the first to open up the space address is lower.
For example, in the following code, variables a and b are stored in the stack, and pointers c and d point to memory space in the heap:
#include using namespace std;int main(){ //stack area to open space, first open the space address high int a = 10; int b = 20; cout
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.