当前位置:网站首页>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;
}
边栏推荐
- Summary of several ways to calculate distance
- scrapy_ Redis distributed crawler
- 小程序直播互动功能运行在App里?
- seaborn数据总体分布的可视化策略
- Annual special analysis of China Mobile Banking in 2022
- SCCM基于已安装的 APP创建客户端集合并定期推送应用更新
- Kubernetes' fast practice and core principle analysis
- Visualization strategy of Seaborn data overall distribution
- Educoder web exercise - validating forms
- Detailed explanation and examples of common parameters of curl
猜你喜欢
MySQL约束(创建表时的各种条件说明)

如何编写测试用例

服务治理的工作内容
![[upgraded student information management system] + file operation + more details](/img/a3/de30e67e1ad73262a9e2cf38e6520c.png)
[upgraded student information management system] + file operation + more details

Hot information of Tami dog: Xiamen property right trading center creates its first time again!

Kubernetes' fast practice and core principle analysis

CVPR2022 | 上科大x小红书首次提出动作序列验证任务,可应用于体育赛事打分等多场景

hands-on-data-analysis 第二单元 第四节数据可视化

《预训练周刊》第50期:无解码变换器、神经提示搜索、梯度空间降维

###数据库的高可用配置(mysql)
随机推荐
【深入理解TcaplusDB技术】TcaplusDB导入数据
Educoder web exercises - form structure
How to read AI summit papers?
对app和微信小程序进行接口测试
Interpretation of tamigou project: 34% equity transfer of Jining Huayuan Project Management Co., Ltd
Is it safe to open a securities account by downloading the app of qiniu business school? Is there a risk?
Educator table labels - settings for Advanced Table Styles
如何使用搜索引擎?
居家辦公初體驗之新得分享| 社區征文
Application configuration management, basic principle analysis
居家办公初体验之新得分享| 社区征文
塔米狗项目解读:济宁华源项目管理有限公司34%股权转让
CVPR2022 | 上科大x小红书首次提出动作序列验证任务,可应用于体育赛事打分等多场景
Kube-prometheus grafana安装插件和grafana-image-renderer
Kubernetes快速實戰與核心原理剖析
华为云发布桌面IDE-CodeArts
Sharing new experiences in home office | community essay solicitation
基于STM32电压检测和电流检测
Test the interface between app and wechat applet
Educator web exercise - grouping form elements