C++程序运行时内存布局之----------局部变量,全局变量,静态变量,函数代码,new出来的变量...

本文详细解析了在VS2010+WindowsXP32位环境下,全局变量、静态变量、局部变量、函数参数及new出来的变量在内存中的布局情况。

声明两点:

(1)开发测试环境为VS2010+WindowsXP32位;

(2)内存布局指的是虚拟内存地址,不是物理地址。

1.测试代码

#include <iostream> using namespace std; int g_int_a; int g_int_b; void f_1() { cout<<"I'm f_1"<<endl; } void f_2() { cout<<"I'm f_2"<<endl; } int main(int argc, char** argv) { int a; int b; static int sa; static int sb; int* h1 = new int; int* h2 = new int; cout<<"argc的地址是 :"<<std::hex<<std::showbase<<&argc<<endl; cout<<"argv的地址是 :"<<std::hex<<std::showbase<<&argv<<endl; cout<<"g_int_a的地址是:"<<std::hex<<std::showbase<<&g_int_a<<endl; cout<<"g_int_b的地址是:"<<std::hex<<std::showbase<<&g_int_b<<endl; cout<<"a的地址是 :"<<std::hex<<std::showbase<<&a<<endl; cout<<"b的地址是 :"<<std::hex<<std::showbase<<&b<<endl; cout<<"f_1()的地址是 :"<<std::hex<<std::showbase<<f_1<<endl; cout<<"f_2()的地址是 :"<<std::hex<<std::showbase<<f_2<<endl; cout<<"main()的地址是 :"<<std::hex<<std::showbase<<main<<endl; cout<<"静态变量sa地址 :"<<std::hex<<std::showbase<<&sa<<endl; cout<<"静态变量sb地址 :"<<std::hex<<std::showbase<<&sb<<endl; cout<<"h1的地地址 :"<<std::hex<<std::showbase<<&h1<<endl; cout<<"h2的地址 :"<<std::hex<<std::showbase<<&h2<<endl; cout<<"new出来*h1地址:"<<std::hex<<std::showbase<<h1<<endl; cout<<"new出来*h2地址:"<<std::hex<<std::showbase<<h2<<endl; cin>>a; }

2.测试结果与布局图

运行结果:

内存分析:

全局变量,静态变量----存于数据区;

局部变量,函数形参----存于stack;

函数代码----------------存于代码区;

new出来的变量--------存于heap。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值