<>C/C++ Memory model

<> Memory model diagram

<> Before program running

After the program is compiled , Generated executable exe file , Before the program is executed, it is divided into two areas .

<>■ Code area :

* Store function body ( Class member function and global function ) Binary code for
* share : For frequently executed programs , Only one copy is needed in memory
* read-only : Prevent programs from accidentally modifying their instructions
<>■ Global area :

* global variable , Static variables
* The global region also includes the constant region ( string constant , Other constants (const modification )) Also in the global area
* characteristic : The data in this area is released by the operating system after the operation of the operating system
<>【 Test global area 】
// Test global area int g_a = 10; int g_b = 10; const int c_g_a = 10; const int c_g_b = 10;
int main() { // Global area : global variable , Static variables , constant // Ordinary local variables int a = 10; int b = 10; cout <<
" local variable a Your address :" << (int)&a << endl; cout << " local variable b Your address :" << (int)&b << endl; cout <<
" global variable g_a Your address :" << (int)&g_a << endl; cout << " global variable g_b Your address :" << (int)&g_b << endl;
// Static variables Add the static keyword Data static variable static int s_a = 10; static int s_b = 10; cout
<< " global variable s_a Your address :" << (int)&s_a << endl; cout << " global variable s_b Your address :" << (int)&s_b <<
endl; // constant string constant cout << " The address of the string constant :" << (int)&"hello world"<< endl; //const
Modified global variables ,const Modified local variables cout << "const Modified global variables c_g_a Your address :" << (int)&c_g_a << endl;
cout<< "const Modified global variables c_g_b Your address :" << (int)&c_g_b << endl; const int c_l_a = 10;
const int c_l_b = 10; cout << "const Modified global variables c_l_a Your address :" << (int)&c_l_a << endl;
cout<< "const Modified global variables c_l_b Your address :" << (int)&c_l_b << endl; }

summary :
■ Not in global zone : local variable ,const Modified local variables ( Local constant )
■ In the global area : global variable ,static Modifying variables ( Static variables ), constant ( string constant ,const Modified global variables ( Global constant ))

<> After running the program

<>■ Stack area :

* Automatically allocated and released by the compiler , A local variable that is allocated by running a function , Function parameters , Return data , Return address
* be careful : Do not return the stack assigned address , The data of stack area is released automatically by compiler
<>■ Heap area :

* Assign release by programmer , If the programmer does not release , At the end of the program, the OS recovery
* C Managing memory usage in language malloc/calloc/realloc and free For memory management
* stay C++ Managing memory usage in new and delete Operator for memory management
* C++ Apply and release space for individual elements in , use new and delete Operator , Apply and release continuous space , use new[] and delete[]
<> Other areas

<>■ Kernel space :

* The program code in this area is not readable or writable
<>■ Memory mapped area :

* Memory mapped area is efficient I/O Mapping mode , Used to load a shared dynamic memory library . Users can use the system interface to create shared data Share memory , Do interprocess communication

Technology
©2019-2020 Toolsou All rights reserved,
tkinter Implementation of user login interface Vue Invalid render dynamic component in subcomponent ( First invalid , Valid for the second time )9 ride 9 Sudoku C/C++ Memory model TypeScript actual combat -12-TS The mechanism of type checking - Type inference Java Thread safety and insecurity JLink,STLink,DAPLink,CMSIS DAP Differences in use Chapter 10 use SpringMVC Frame transformation of supermarket order management system -1C Language calculation of deposit interest About the Blue Bridge Cup , Things you should know !