程序中变量的内存安排和const_cast问题

结论:

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






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值