当前位置:网站首页>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;
}
边栏推荐
- 5000 word analysis: the way of container security attack and defense in actual combat scenarios
- Tami dog sharing: the way of property right transaction and the significance of data-based property right transaction market
- [Anxin cup 2019]easy_ web-1
- uva11292
- 《預訓練周刊》第50期:無解碼變換器、神經提示搜索、梯度空間降維
- How to use search engine?
- Huawei cloud releases desktop ide codearts
- Centos7 deploying MySQL environment
- Questions and answers No. 43: application performance probe monitoring principle PHP probe
- Kubernets Rapid Practical fighting and Core Principle Analysis
猜你喜欢

Not only zero:bmtrain technology principle analysis

Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关

服务治理的工作内容

【深入理解TcaplusDB技术】TcaplusDB导入数据
![[安洵杯 2019]easy_web-1](/img/1d/f164c220f6c8e98b981ef79b0e96bc.png)
[安洵杯 2019]easy_web-1

2022年中国手机银行年度专题分析

还在用generator生成xxx管理系统的CRUD代码?来看看我是怎么写的

Educoder web exercise - validating forms

《預訓練周刊》第50期:無解碼變換器、神經提示搜索、梯度空間降維
![[upgraded student information management system] + file operation + more details](/img/a3/de30e67e1ad73262a9e2cf38e6520c.png)
[upgraded student information management system] + file operation + more details
随机推荐
Deep understanding of convolution in convolution neural network
Detailed explanation and examples of common parameters of curl
Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection
Is it safe to open a securities account by downloading the app of qiniu business school? Is there a risk?
如何阅读AI顶会论文?
Implementation principle and application practice of Flink CDC mongodb connector
Cvpr2022 | the action sequence verification task was first proposed by X xiaohongshu of Shanghai University of science and technology, which can be applied to multiple scenarios such as scoring of spo
【深入理解TcaplusDB技术】Tmonitor后台一键安装
PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
MySQL约束(创建表时的各种条件说明)
2022年中国手机银行年度专题分析
Six possible challenges when practicing Devops
[安洵杯 2019]easy_web-1
Not only zero:bmtrain technology principle analysis
塔米狗项目解读:济宁华源项目管理有限公司34%股权转让
curl常用参数详解及示例
Efficient remote office manual | community essay solicitation
seaborn绘图风格的设置
An error "\.\global\vmx86" is reported when vmware12 virtual machine is opened: the system cannot find the specified file.
UVA1203 Argus