程序中变量的内存安排和const_cast问题
2013-06-16 23:18
337 查看
结论:
【1】const_cast可以转换栈和堆中的const,但对静态存储区中的const无能为力。
【2】bss段的排布顺序是: 1)全局non-static未初始化变量;2)全局static未初始化变量;3)局部static未初始化变量
【3】data数据段的排布规则是:1)全局已初始化变量,不分是否static,只按声明顺序;2)局部static已初始化变量。
【4】const全局变量(const;static const)或局部static const 变量放在一起,位于【3】之前,是只读区,排列顺序是:1)全局const或static const变量(不区分是否static);2)局部static const变量。
从低到高依次是【4】、【3】、【2】、堆、栈
PS:另外,测试代码没有体现的是,在栈中,如果没有函数调用,只是if{}或者{}或者while{},那么是不会重新开启一块堆栈的,分配依然会在原有地址基础上向上扩展,只有函数调用才会新建栈帧。
测试代码:
【1】const_cast可以转换栈和堆中的const,但对静态存储区中的const无能为力。
【2】bss段的排布顺序是: 1)全局non-static未初始化变量;2)全局static未初始化变量;3)局部static未初始化变量
【3】data数据段的排布规则是:1)全局已初始化变量,不分是否static,只按声明顺序;2)局部static已初始化变量。
【4】const全局变量(const;static const)或局部static const 变量放在一起,位于【3】之前,是只读区,排列顺序是:1)全局const或static const变量(不区分是否static);2)局部static const变量。
从低到高依次是【4】、【3】、【2】、堆、栈
PS:另外,测试代码没有体现的是,在栈中,如果没有函数调用,只是if{}或者{}或者while{},那么是不会重新开启一块堆栈的,分配依然会在原有地址基础上向上扩展,只有函数调用才会新建栈帧。
测试代码:
#include <iostream> using namespace std; int ig1=8; int ig2=8; const int igc1=6; const int igc2=6; static const int igcs1=7; static const int igcs2=7; static int igs1=9; static int igs2=9; int ig3=8; const int igc3=6; static const int igcs3=7; static int igs3=9; int g1; int g2; static int gs1; static int gs2; int g3; static int gs3; int main() { int y1=3; int y2=3; const int cy1=4; const int cy2=4; static int sy1=5; static int sy2=5; static int nsy1; static int nsy2; static const int scy1=2; static const int scy2=2; int y3=3; const int cy3=4; static int sy3=5; static int nsy3; static const int scy3=2; { int ly1=3; const int cly1=3; int ly2=3; const int cly2=cly1; const int cly3=cly1; int ly3=3; /* // {} cout<<"ly1= "<<ly1<<" "<<&ly1<<endl; cout<<"ly2= "<<ly2<<" "<<&ly2<<endl; cout<<"ly3= "<<ly3<<" "<<&ly3<<endl; cout<<"cly1= "<<cly1<<" "<<&cly1<<endl; cout<<"cly2= "<<cly2<<" "<<&cly2<<endl; cout<<"cly3= "<<cly3<<" "<<&cly3<<endl; */ } /* //main cout<<"y1= "<<y1<<" "<<&y1<<endl; cout<<"y2= "<<y2<<" "<<&y2<<endl; cout<<"y3= "<<y3<<" "<<&y3<<endl; cout<<"cy1= "<<cy1<<" "<<&cy1<<endl; cout<<"cy2= "<<cy2<<" "<<&cy2<<endl; cout<<"cy3= "<<cy3<<" "<<&cy3<<endl; cout<<"sy1= "<<sy1<<" "<<&sy1<<endl; cout<<"sy2= "<<sy2<<" "<<&sy2<<endl; cout<<"sy3= "<<sy3<<" "<<&sy3<<endl; cout<<"scy1= "<<scy1<<" "<<&scy1<<endl; cout<<"scy2= "<<scy2<<" "<<&scy2<<endl; cout<<"scy3= "<<scy3<<" "<<&scy3<<endl; // g cout<<"g1= "<<g1<<" "<<&g1<<endl; cout<<"g2= "<<g2<<" "<<&g2<<endl; cout<<"g3= "<<g3<<" "<<&g3<<endl; cout<<"gs1= "<<gs1<<" "<<&gs1<<endl; cout<<"gs2= "<<gs2<<" "<<&gs2<<endl; cout<<"gs3= "<<gs3<<" "<<&gs3<<endl; cout<<"ig1= "<<ig1<<" "<<&ig1<<endl; cout<<"ig2= "<<ig2<<" "<<&ig2<<endl; cout<<"ig3= "<<ig3<<" "<<&ig3<<endl; cout<<"igc1= "<<igc1<<" "<<&igc1<<endl; cout<<"igc2= "<<igc2<<" "<<&igc2<<endl; cout<<"igc3= "<<igc3<<" "<<&igc3<<endl; cout<<"igs1= "<<igs1<<" "<<&igs1<<endl; cout<<"igs2= "<<igs2<<" "<<&igs2<<endl; cout<<"igs3= "<<igs3<<" "<<&igs3<<endl; cout<<"igcs1= "<<igcs1<<" "<<&igcs1<<endl; cout<<"igcs2= "<<igcs2<<" "<<&igcs2<<endl; cout<<"igcs3= "<<igcs3<<" "<<&igcs3<<endl; cout<<"nsy1= "<<nsy1<<" "<<&nsy1<<endl; cout<<"nsy2= "<<nsy2<<" "<<&nsy2<<endl; cout<<"nsy3= "<<nsy3<<" "<<&nsy3<<endl; */ int *p1=new int; const int *p2=new int; int *const p3=new int[3]; cout<<"p1= 1 "<<p1<<endl; cout<<"p2= 1 "<<p2<<endl; cout<<"p3= 1 "<<p3<<endl; int *p; p = const_cast<int*>(&cy1); // 栈的const *p=9; cout<<*p<<endl; // p=const_cast<int*>(&igcs1); *p=9; cout<<*p<<endl;//全局,static const.编译通过,运行段错误 // p=const_cast<int*>(&igc1); *p=9; cout<<*p<<endl; //全局,static const。编译通过,运行段错误 // p=const_cast<int*>(&scy1); *p=9; cout<<*p<<endl; //局部,static const。编译通过,运行段错误 p=const_cast<int*>(p2); *p=9; cout<<*p<<endl;//堆中 const int * p=const_cast<int*>(p3); p=&ig1; cout<<*p<<endl; //堆中 const *int delete p1; delete p2; delete []p3; return 0; }
相关文章推荐
- 使用const_cast转换全局const变量导致程序崩溃的原因
- 分享:程序笔试题-const变量通过指针修改问题
- C++程序变量在内存中的分配问题
- 程序笔试题-const变量通过指针修改问题
- 程序笔试题-const变量通过指针修改问题
- 探讨:程序在内存中的分配(常量,局部变量,全局变量,程序代码)问题
- 探讨:程序在内存中的分配(常量,局部变量,全局变量,程序代码)问题
- 一次由于内存问题程序被kill的测试 (4)
- 检测ios程序内存问题
- C++类对象成员变量和成员函数内存分配的问题
- JavaScript高级程序设计——第4章:变量、作用域和内存问题
- 对于全局变量对应的内存分配问题的追踪
- 解决在url中传递js变量到java程序中出现乱码问题(转自http://www.blogjava.net/fine)
- Linux 下C语言学习(四)——变量的类型和程序各部分代码在内存中的位置
- C++类对象成员变量与成员函数内存分配问题
- 学习 javascript高级程序设计 (第3版)-- 变量、作用域、内存问题
- javascript高级程序设计第三版 第四章 变量、作用域和内存问题
- C++类对象成员变量与成员函数内存分配问题
- 探讨C++ 变量生命周期、栈分配方式、类内存布局、Debug和Release程序的区别2
- 每天学一点flash(24) air程序内存问题