c++中const变量真的不可以修改吗?

c++中const变量真的不可以修改吗?

在学c++的时候,看到的大部分的书籍对const关键字的介绍都是:const关键字修饰的变量的值是不可被修改的。但是事实真是如此吗?今天做了一个小的实验,发现const变量是可以被修改的。c++代码如下:           
 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     const int a = 3;
 7     int* p = const_cast<int*>(&a);
 8     *= 4;
 9     cout << "value of p: " << *<< endl;
10     cout << "value of a: " << a << endl;
11     cout << "address of p: " << p << endl;
12     cout << "address of a: " << &<< endl;
13 
14     return 0;
15 }             
上面代码第7行将a的地址赋值给指针p,然后第8行修改p所指向的地址中的值,运行结果如下:
运行结果
value of p: 4
value of a: 3
address of p: 0x7fbffff7fc
address of a: 0x7fbffff7fc
如上所示结果,指针p所指向的地址和变量a的地址相同,但是p所指地址中的值已经发生改变。但是为何变量a对应的地址中的值已经由3变为4,但是a的值确实3呢?
暂时把这个问题搁浅。再来看一下如下的c++代码:
 1 #include <iostream>
 2 using namespace std;
 3 const int a = 3;
 4 int main()
 5 {
 6     //const int a = 3;
 7     int* p = const_cast<int*>(&a);
 8     *= 4;
 9     cout << "value of p: " << *<< endl;
10     cout << "value of a: " << a << endl;
11     cout << "address of p: " << p << endl;
12     cout << "address of a: " << &<< endl;
13 
14     return 0;
15 }
如上代码g++编译通过,在运行时报错如下:
输出结果
Segmentation fault (core dumped)
由此可见,在c++中全局const变量和局部const变量的编译器处理的方法是不一样的。查阅资料得知,全局const变量是不分配内存地址的,它编译器放置在符号表中作为编译期常量,全局const变量放在只读数据段中,受到只读数据段的权限保护,当你修改一个只读数据段中的内容时,会得到一个运行时错误。而局部const变量是放在堆栈之中,因为在内存中有地址,通过修改地址中的值可以达到修改const所指内存中值的目的。

posted on 2012-04-10 23:49 MrRightLeft 阅读(3736) 评论(10)  编辑 收藏 引用 所属分类: C/C++

评论

# re: c++中const变量真的不可以修改吗? 2012-04-11 13:16 迎风而立

这个修改的并不是a的值啊,而是*p的值。*p和a表示同一个单元。但是当用3给a赋值之后,是a的值不可以改变,而不是*p的值不可以改变。  回复  更多评论   

# re: c++中const变量真的不可以修改吗?[未登录] 2012-04-11 14:10 ithaca

博主还是没能解释为什么第一个程序中a的值没有变为4  回复  更多评论   

# re: c++中const变量真的不可以修改吗? 2012-04-11 14:20 OUR!!CPP

const_cast<int*>这里已经告诉编译器把原本放在常量区的变量移出来了。  回复  更多评论   

# re: c++中const变量真的不可以修改吗? 2012-04-11 16:28 so

cout << "value of a: " << a << endl;
\\
cout << "value of a: " << 3 << endl;  回复  更多评论   

# re: c++中const变量真的不可以修改吗? 2012-04-12 18:09 MrRightLeft

@OUR!!CPP
在const_cast<int*>之前打印出a的地址,和应用了const_cast<int*>之后的a的地址比较,是相同的。何来变量被从常量区移出来一说?
  回复  更多评论   

# re: c++中const变量真的不可以修改吗? 2012-04-12 18:12 MrRightLeft

a这个常量不是从相应地址中取出来的。在程序运行时a的值应该是从符号表中取出。所以改变a对应地址中的值,并不会改变a变量的值@ithaca
  回复  更多评论   

# re: c++中const变量真的不可以修改吗? 2012-04-17 11:00 Coneagoe

#include <iostream>
using namespace std;

int main() {
const int N = 22;
int * pN = const_cast<int*>(&N);
*pN = 33;
cout << N << '\t' << &N << endl;
cout << *pN << '\t' << pN << endl;
}
Output:

22 0xbf91cfa0
33 0xbf91cfa0


The above output, obtained with gcc version 4.0.3, could be different on your system, because the behavior is undefined.

In this example, we used const_cast to obtain a regular pointer to a const int. Because the const int is in stack storage class, we don't get a segmentation fault by attempting to change the memory. The compiler is unable to "optimize out" the int, and the const_cast tells it not to even try.

Ref:
http://cartan.cas.suffolk.edu/oopdocbook/html/staticcast.html  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜