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 memory partitions of C++ programs?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The editor would like to share with you what the memory partitions of C++ programs are, which I believe most people do not know very well, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

The memory area occupied by C++ programs can be divided into five regions: stack memory area, heap memory area, global / static memory area, text constant memory area and program code area.

The following use of daily development programming examples, a detailed introduction of these five partitions, so that we can have a deeper understanding of these five major memory partitions.

1. Stack memory area

The stack memory area is the partition we use most, and it will be used wherever there are functions. Stack partition is a memory area used to store function parameters and local variable values, which is automatically allocated and released by the compiler at compile time.

The memory occupied by the parameters in the function and the local variables in the function is allocated when the code executes to the function (entering the function), and the memory is automatically released when leaving the function. Here are a few simple examples to learn more about stack memory.

1.1. Pass the parameter value of the function through the stack when calling the function

When a function is called, the parameter values are passed through the stack, that is, the parameter values of the function are pressed onto the stack before calling the function, and then call the called function. This can be clearly seen in the assembly code. For example, the following is a simple code to add two numbers:

/ / the called function int AddNum (int a, int b) {int nSum = a + b; return nSum;} / / the instance code of the function called within the call int a = 7 interint b = 8; int nSum = AddNum (a, b)

You can view the assembly code corresponding to the above C++ code in VS. To do this, copy the above code into VS, start VS debugging, and click "go to disassembly" area in the right-click menu displayed to view the assembly code corresponding to C++ code:

As you can see from the assembly code above, the values of parameters an and b to be passed in are pressed on the stack before calling the AddNum function, and then go to the call AddNum function. AddNum, as the called function, reads the contents of the incoming parameters from the stack.

1.2. There is an upper limit on the stack memory occupied by threads.

There is an upper limit to the stack memory occupied by a thread, and you can specify the size of the stack space when you create the thread. On Windows, the default stack space for threads is 1MB. The total stack space occupied by all the functions in the function call stack of a thread at a certain time is the stack memory occupied by the thread at the current time.

When entering the function, the stack space of the function will be accumulated to the memory occupied by the thread (the stack space of the local variables is applied for within the function), while leaving the function will release the stack space it occupies, which will subtract the space occupied by the function from the stack memory occupied by the thread. If the stack space occupied by the current thread is greater than the upper limit of the thread (usually triggered when entering a function), a stack overflow exception of "stack overflow" will be reported:

The program will crash.

It should be pointed out here that the switch...case statement is used in a function, which contains several case branches, and some local variables are defined in these branches. Although the life cycle of these local variables is only in the case clause, they are all calculated directly on the stack space of the function, and are allocated as soon as the function is executed, even if it has not been run into the corresponding case clause yet. Even if the life cycle of the local variables of these case clauses is only within the case clause!

2. Heap memory area

Heap memory is also our most commonly used memory area, because the stack memory of each thread is limited, we usually put most of the data in heap memory.

In C++, the memory requested by malloc/new is allocated from the heap memory and is freed by the free/delete area after use. If heap memory is not freed, the program is uniformly reclaimed by the operating system at the end of the program.

The management of heap memory is much more complex than stack memory. If it is a crash caused by a heap memory exception, it is much more difficult to find than a crash caused by a stack memory exception (for example, memory access caused by memory out of bounds).

If the memory from malloc/new is not released after running out, it will cause a memory leak. If there is a memory leak in the frequently executed code, it is fatal, because as the running time of the program increases, more and more memory leaks will occur. If the virtual memory of the process is exhausted, a "Out of memory" exception will be generated:

The program directly flashed and crashed.

3. Global / static memory area

The memory of global variables and static variables is allocated in this area, and the life cycle of global variables and static variables is the same, both allocate memory when the program starts and release memory when the program exits.

Global variables are typically declared using the extern keyword, such as:

Extern int m_nClientId

Static variables are declared using the static keyword:

Static int nCount

Both global and static variables are required to be initialized when they are defined. Note that the definition here is the concept that corresponds to the declaration. The difference between global variables and static variables is that global variables have a wider scope and can be used throughout the module. Static variables have different scopes depending on where they are defined.

You can define static variables in a function or static member variables in a class. Static variables defined in a function can only be accessed in a function, while static variables defined in a class can be accessed outside the class using "class name:: static member variable name".

4. Text constant area

This partition is used to store constant values, such as constant strings, such as the following string constants: (assign the address of the string constant to the pointer p):

Char* p = "this is a test."

The memory address occupied by the string is stored in the text constant area.

The contents of this part of the memory are fixed constants and are not allowed to be modified, and will be uniformly recycled by the operating system after the end of the program. This part is relatively simple, there is nothing to talk about.

5. Program code area

The memory mentioned above is the memory of the data segment, which is used to store all kinds of data in the running of the program; the memory of this part is the memory of the code segment, which is used to store the binary code of the program.

The memory address of the data segment and the address of the code snippet instruction are completely two concepts and cannot be confused. For example, the memory address of a variable is the address of the data segment:

The address of an assembly instruction is the address of the code snippet:

They are two addresses that have nothing to do with each other, so be sure to distinguish them.

These are all the contents of this article entitled "what are the memory partitions of C++ programs?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report