当前位置:网站首页>1. memory partition model

1. memory partition model

2022-06-21 13:16:00 I want to go sailing

C++ Program in execution , The main direction of memory is divided into 4 Regions :

1. Code section : Store the binary code of the function body , Managed by the operating system .
2. Global area : Store global and static variables and constants .
3. The stack area : Release is automatically allocated by the compiler , Stores the parameter values of the function , Local variables, etc .
4. Heap area : Assigned and released by the programmer , If programmers don't release , Recycle by the operating system at the end of the program .

The meaning of four memory areas : Data stored in different areas , Give different life cycles , Give us greater flexibility in programming .

1.1 Before the program runs

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

Code section :

1. Deposit CPU Machine instructions executed .
2. The code area is shared , The purpose of sharing is for programs that are frequently executed , Just have a copy of the code in memory .
3. The code area is read-only , The reason to make it read-only is to prevent the program from accidentally modifying its instructions .

Global area :

1. Global variables and static variables are stored here .
2. The global area also includes the constant area , String constants and other constants are also stored here .
3. The data in this area is released by the operating system at the end of the program .

Not in the global area In the global area
local variable Global variables ; Static variables :static keyword
const Modified local variables ( Local constants ) Constant : String constant ;const Decorated global variables ( Global constants )

Example :

#include<iostream>
using namespace std;

//  Create global variables 
int g_a=10; 

int main()
{
    
	//  Global area : Global variables , Static variables , Constant 
	
	//  Create ordinary local variables 
	int a=10;
	
	//  Static variables   Add... Before ordinary variables static, It's a static variable 
	static int s_a=10;
	
	//  Constant : String constant  const Decorated global variables  
	
	system("pause");
	return 0;
}

1.2 After program running

The stack area :

1. Release is automatically allocated by the compiler , Stores the parameter values of the function , Local variables, etc .
2. matters needing attention : Do not return the address of a local variable , The data opened in the stack area is automatically released by the compiler .

Example :

#include<iostream>
using namespace std;

int* func()
{
    
	int a=10;			//  local variable , Store in the stack area , The data in the stack area is automatically released after the function is executed 
	return &a;			//  Returns the address of a local variable  
}

int main()
{
    
	//  Accept func The return value of the function 
	int* p = func();
	
	cout<<*p<<endl; 		//  You can print the right numbers for the first time , It's because the compiler has reserved  
	//cout<<*p<<endl; //  The second time, the data is no longer kept  
	
	system("pause");
	return 0;
}

Heap area :

1. Release is assigned by the programmer , If programmers don't release , Recycle by the operating system at the end of the program .
2. stay C++ The main use of new Open up memory in the heap .

Example :

#include<iostream>
using namespace std;

int* func()
{
    
	//  utilize new keyword   Data can be opened up to the heap 
	int* p = new int;
	*p=10;
	return p; 
}

int main()
{
    
	//  Open up data in the heap 
	int* p = func();
	cout<<*p<<endl;
	cout<<*p<<endl; 
	system("pause");
	return 0;
}

1.3 new The operator

C++ In the use of new The operator creates data in the heap .

Data from the development of the reactor area , It's created manually by the programmer , Hand release , Release utilization operator delete.

grammar :new data type

utilize new Data created , Will return a pointer to the type corresponding to the data .

Example :

#include<iostream>
using namespace std;

//  Utilization in the reactor area new Open up arrays 
void test02()
{
    
	//  establish 10 An array of integer data , In the pile area 
	int* arr=new int[10];
	for(int i=0; i<10; i++)
	{
    
		arr[i] = i+100;	
	}	
	for(int j=0; j<10; j++)
	{
    
		cout<<arr[j]<<endl; 
	}
	
	//  Free heap array 
	//  When releasing the array , To add [] Can only be 
	delete []arr; 
} 

int main()
{
    
	test02();
	
	system("pause");
	return 0;
}
原网站

版权声明
本文为[I want to go sailing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211206238492.html