C++ 程序运行前内存分区模型

C++核心编程
本阶段主要针对C++面向对象编程技术做详细讲解,探讨C++中的核心和精髓。
1、内存分区模型
C++程序在执行时,将内存大方向划分为4个区域
●代码区:存放函数体的二进制代码,由操作系统进行管理的
●全局区:存放全局变量和静态变量以及常量
●栈区:|由编译器自动分配释放,存放函数的参数值,局部变量等
●堆区:由程序员分配和释放若程序员不释放程序结束时由操作系统回收
内存四区意义:
不同区域存放的数据,赋予不同的生命周期,给我们更大的灵活编程

1.1程序运行前
在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域
代码区:
存放CPU执行的机器指令
代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可
代码区是只读的,使其只读的原因是防止程序意外地修改了它的指令
全局区:
全局变量和静态变量存放在此.
全局区还包含了常量区,字符串常量和其他常量也存放在此.
该区域的数据在程序结束后由操作系统释放.

#include<iostream>
#include<string>
using namespace std;

int main() {

	//创建普通局部变量
	int a = 10;
	int b = 10;
	cout << "局部变量a的地址为:" << (int)&a << endl;
	cout << "局部变量b的地址为:" << (int)&b << endl; 
	system(" pause") ;

}

全局变量:

#include<iostream>
#include<string>
using namespace std;


int g_a = 10;
int g_b = 10;

int main() {

	//创建普通局部变量
	int a = 10;
	int b = 10;
	cout << "局部变量a的地址为:" << (int)&a << endl;
	cout << "局部变量b的地址为:" << (int)&b << endl; 

	cout << "全局变量g_a的地址为 : " << (int)&g_a << endl;
	cout << "全局变量g_b的地址为 : " << (int)&g_b << endl;

	system(" pause") ;

}

局部变量的内存地址和全局变量的内存地址明显不不一块区域

静态变量:

#include<iostream>
#include<string>
using namespace std;


int g_a = 10;
int g_b = 10;

int main() {

	//创建普通局部变量
	int a = 10;
	int b = 10;
	cout << "局部变量a的地址为:" << (int)&a << endl;
	cout << "局部变量b的地址为:" << (int)&b << endl; 

	cout << "全局变量g_a的地址为 : " << (int)&g_a << endl;
	cout << "全局变量g_b的地址为 : " << (int)&g_b << endl;


	//静态变量   在普通变量前用static关键字修饰
    static	int g_a = 10;
	static  int g_b = 10;
	cout << "静态变量g_a的地址为 : " << (int)&g_a << endl;
	cout << "静态变量g_b的地址为 : " << (int)&g_b << endl;


	system(" pause") ;

}

 

字符串常量:

#include<iostream>
#include<string>
using namespace std;


int g_a = 10;
int g_b = 10;

int main() {

	//创建普通局部变量
	int a = 10;
	int b = 10;
	cout << "局部变量a的地址为:" << (int)&a << endl;
	cout << "局部变量b的地址为:" << (int)&b << endl; 

	cout << "全局变量g_a的地址为 : " << (int)&g_a << endl;
	cout << "全局变量g_b的地址为 : " << (int)&g_b << endl;


	//静态变量   在普通变量前用static关键字修饰
    static	int g_a = 10;
	static  int g_b = 10;
	cout << "静态变量g_a的地址为 : " << (int)&g_a << endl;
	cout << "静态变量g_b的地址为 : " << (int)&g_b << endl;


	//常量
    //字符串常量
	cout << "he1lo world字符串常量的地址为:" << (int)&"he1lo world" << endl;
	//const修饰的变量


	system(" pause") ;

}

#include<iostream>
#include<string>
using namespace std;


int g_a = 10;
int g_b = 10;
 
const int c_g_a = 10;  //const修饰的全局变量  全局的常量
const int c_g_b = 10;


int main() {

	//创建普通局部变量
	int a = 10;
	int b = 10;
	cout << "局部变量a的地址为:" << (int)&a << endl;
	cout << "局部变量b的地址为:" << (int)&b << endl; 

	cout << "全局变量g_a的地址为 : " << (int)&g_a << endl;
	cout << "全局变量g_b的地址为 : " << (int)&g_b << endl;


	//静态变量   在普通变量前用static关键字修饰
    static	int g_a = 10;
	static  int g_b = 10;
	cout << "静态变量g_a的地址为 : " << (int)&g_a << endl;
	cout << "静态变量g_b的地址为 : " << (int)&g_b << endl;


	//常量
    //字符串常量
	cout << "he1lo world字符串常量的地址为:" << (int)&"he1lo world" << endl;

	//const修饰的变量
	//const修饰的全局变量,const修 饰的局部变量、
	cout << " 全局常量 c_g_a的地址为 : " << (int)&c_g_a << endl;
	cout << " 全局常量 c_g_b的地址为 : " << (int)&c_g_b << endl;

	system("pause") ;

}

 全局常量:

 

#include<iostream>
#include<string>
using namespace std;


int g_a = 10;
int g_b = 10;
 
const int c_g_a = 10;  //const修饰的全局变量  全局的常量
const int c_g_b = 10;


int main() {

	//创建普通局部变量
	int a = 10;
	int b = 10;
	cout << "局部变量a的地址为:" << (int)&a << endl;
	cout << "局部变量b的地址为:" << (int)&b << endl; 

	cout << "全局变量g_a的地址为 : " << (int)&g_a << endl;
	cout << "全局变量g_b的地址为 : " << (int)&g_b << endl;


	//静态变量   在普通变量前用static关键字修饰
    static	int g_a = 10;
	static  int g_b = 10;
	cout << "静态变量g_a的地址为 : " << (int)&g_a << endl;
	cout << "静态变量g_b的地址为 : " << (int)&g_b << endl;


	//常量
    //字符串常量
	cout << "he1lo world字符串常量的地址为:" << (int)&"he1lo world" << endl;

	//const修饰的变量
	//const修饰的全局变量,const修 饰的局部变量、
	cout << " 全局常量 c_g_a的地址为 : " << (int)&c_g_a << endl;
	cout << " 全局常量 c_g_b的地址为 : " << (int)&c_g_b << endl;


	const int c_l_a = 10; // c-const  g - global 1 - 1ocal
	const int c_l_b = 10;
	cout << " 局部常量c_l_a的地址为 : " << (int)&c_l_a << endl;
	cout << " 局部常量c_l_b的地址为 : " << (int)&c_l_b << endl;

	system("pause") ;

}

总结:

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值