当前位置:网站首页>Memory partition model

Memory partition model

2022-06-26 16:35:00 Meaauf

Memory partition model

  • Code section : Store the binary code of the function body , Managed by the operating system
  • Global area : Store global and static variables and constants
  • The stack area : Automatically assigned by the compiler , Stores the parameter values of the function , Local variables, etc
  • Heap area : Assigned and released by the programmer , If programmers don't release , Recycle by the operating system at the end of the program

Before the code runs

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 , You only need a bit of code in memory
  3. The code area is read-only , The purpose is to prevent the program from accidentally modifying instructions

Global area

  1. Global variables and static variables are stored here
  2. The global area also contains constants 、 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
#include <iostream>
using namespace std;

int global_a=10; //  Global variables 
const int const_global_a=10;  // const Modified global constant 

int main(void)
{
    
    int a=10;  //  local variable 
    const int const_a=10;  // const Modified local constants 
    static int b=10; //  Static variables 
    cout << "Hello" << endl; // "Hello"  String constant 
    return 0;
}

 Illustrate global and non global areas

summary

  • C++ Before the program runs, it is divided into global area and code area
  • Code areas are characterized by shared and read-only
  • Global variables are stored in the global area 、 Static variables 、 Constant
  • In the constant area const Modified global variables and string constants

After the code runs

The stack area

  1. Release is automatically allocated by the compiler , Stores the parameter values of the function 、 Local variables, etc

Do not try to return the address of a local variable , The space opened up by the stack area will be automatically released by the compiler

#include <iostream>
using namespace std;

int* f()
{
    
    int a=10;
    return &a;
}

int main(void)
{
    
    int*p=f(); // Try to accept a local variable of a function 
    
    cout << *p << endl;
    cout << *p << endl;
    cout << *p << endl;
    return 0;
}

 The stack area

Heap area

new Allocate space

int * arr=new int[10];  //  establish 10 An array of shaped data 
delete[] arr;  //  Free heap array 

int * a=new int(10); //  Create value for 10 The shaping variable of 
delete(a);
  1. Release is assigned by the programmer , If programmers don't release , Recycle by the operating system at the end of the program
  2. adopt new Open up space in the stack area ( amount to C Medium malloc)
#include <iostream>
using namespace std;

int* f(int n)
{
    
	return new int(n);  //  Allocate space  n: Generated int Initial value 
}

int main(void)
{
    
	int * p=f(10);
	cout << *p << endl;
	cout << *p << endl;
	cout << *p << endl;   //  If you don't release it manually , The value will always exist 
    delete(p);  //  Free up space created by heap area 
	cout << *p << endl;
	cout << *p << endl;
	cout << *p << endl;
	return 0;
}

 Heap area

原网站

版权声明
本文为[Meaauf]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261620553354.html

随机推荐