<>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,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element