Get the App
SLTechnology News&Howtos  ›  Development  › 

How to manage the memory of CAccord + memory

Shulou Source: shulou.com Published: 2022-05-31 21:59:33 09月21日 Update

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

Tags: Space memory function type dynamic address size object management system individual program resource function operator code principle actual pointer mode Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Huawei Microsoft Shulou Tech Info