当前位置:网站首页>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
- Deposit CPU Machine instructions executed
- 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
- The code area is read-only , The purpose is to prevent the program from accidentally modifying instructions
Global area
- Global variables and static variables are stored here
- The global area also contains constants 、 String constants and other constants are also stored here
- 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;
}

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
- 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;
}

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);
- Release is assigned by the programmer , If programmers don't release , Recycle by the operating system at the end of the program
- 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;
}

边栏推荐
- Ideal path problem
- 数据分析----numpy快速入门
- Redis order sorting command
- When a programmer is disturbed 10 times a day, the consequences are amazing!
- JS tutorial electron JS is a good tool for designing powerful multi platform desktop applications
- r329(MAIX-II-A(M2A)资料汇总
- C# 读写文件从用户态切到内核态,到底是个什么流程?
- Mono 的一些实例方法
- 板卡的分级调试经验
- 基于Kubebuilder开发Operator(入门使用)
猜你喜欢

【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)

100+ data science interview questions and answers Summary - basic knowledge and data analysis

100+数据科学面试问题和答案总结 - 基础知识和数据分析

1-12vmware adds SSH function

JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序

基于Kubebuilder开发Operator(入门使用)

油田勘探问题

When a programmer is disturbed 10 times a day, the consequences are amazing!

大话领域驱动设计——表示层及其他
![[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap](/img/b6/21e51fa7f79d4a4b950f061703f0fb.png)
[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap
随机推荐
Research on natural transition dubbing processing scheme based on MATLAB
Keepalived 实现 Redis AutoFailover (RedisHA)
[从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步
当一个程序员一天被打扰 10 次,后果很惊人!
pybullet机器人仿真环境搭建 5.机器人位姿可视化
请指教同花顺软件究竟是什么?网上开户是否安全么?
用Attention和微调BERT进行自然语言推断-PyTorch
基於Kubebuilder開發Operator(入門使用)
LeetCode Algorithm 24. 两两交换链表中的节点
LeetCode Algorithm 24. Exchange the nodes in the linked list in pairs
r329(MAIX-II-A(M2A)资料汇总
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity
What does the inner structure of the neural network "alchemy furnace" look like? An interpretation of the thesis by the doctor of Oxford University
构造函数和析构函数
【力扣刷题】11.盛最多水的容器//42.接雨水
R language uses cor function to calculate the correlation matrix for correlation analysis, uses corrgram package to visualize the correlation matrix, reorders rows and columns using principal componen
proxy
What is flush software? Is it safe to open an account online?
网页课程设计大作业——华山旅游网
[Li Kou brush questions] 11 Container holding the most water //42 Rain water connection