注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。
测试环境:Ubuntu 10.10
GCC版本:4.4.5
一、C语言中的const
1)const修饰的变量是只读的,本质还是变量
2)const修饰的局部变量在栈上分配空间
3)const修饰的全局变量在只读存储区分配空间
4)const只在编译期有用,在运行期无用
const修饰的变量不是真的常量,它只是告诉编译器该变量不能出现在赋值符号的左边
5)C语言中的const使得变量具有只读属性
6)const将具有全局生命周期的变量存储于只读存储区(如果修改全局变量会导致程序崩溃,gcc编译器编译正常,运行会产生段错误)
#include <stdio.h>
const int a = 10;
int main(void)
{
//const int a = 10;
int *p = (int*)&a;
*p = 15;
printf("*p = %d\n",*p);
printf("a = %d\n",a);
return 0;
}
操作:
1)编译:gcc test.c -o test.out。运行结果:
Segmentation fault (core dumped)。
2)修改代码:
#include <stdio.h>
int main(void)
{
const int a = 10;
int *p = (int*)&a;
*p = 15;
printf("*p = %d\n",*p);
printf("a = %d\n",a);
return 0;
}
编译:gcc test.c -o test.out。运行结果:
*p = 15
a = 15
C语言中:const不能定义真正意义上的常量!
编程实验
C/C++中的const
3-1.cpp
#include <stdio.h>
int main()
{
const int c = 0; //C++下存放在符号表。因为没有分配空间。
int* p = (int*)&c; //注意类型匹配,否则会出警告
printf("Begin...\n");
*p = 5;
printf("c = %d\n", c);
//printf("*p = %d\n",*p); //修改一
printf("End...\n");
return 0;
}
操作:
1) C++语言编写,g++ 3-1.cpp -o 3-1.out编译正常。
打印结果:
Begin...
c = 0
End...
分析:C++:变量c进入了符号表,无法通过指针方式修改c的数据。
C语言编写,gcc 3-1.c -o c3-1.out编译正常。
打印结果:
Begin...
c = 5
End...
分析:C:变量c被分配内存,通过指针指向内存,解引用可以修改c数据。
二、C++中的const
1)C++在C的基础上对const进行了进化处理(重点)
- 当碰见const声明时在符号表中放入常量(字面量初始化时,const修饰的变量为常量)
- 编译过程中若发现使用常量则直接以符号表中的值替换
- 编译过程中若发现下述情况则给对应的常量分配存储空间
1、对const常量使用了extern
2、对const常量使用&操作符
注意:
C++编译器虽然可能为const常量分配
空间,但不会使用其存储空间中的值。(真正意义上常量)
符号表和指针对应的不同存储空间,导致显示数据不同。
2)C语言中的const变量
- C语言中const变量是只读变量,会分配存储空间
3)C++中的const常量(如果不分配内存就是常量)
- 可能分配存储空间
1、当const常量为全局,并且需要在其它文件中使用(extern在其它文件声明)
2、当使用&操作符对const常量取地址
3、const修饰的变量定义时,被其它变量初始化。
const修饰的变量定义时,被其它变量初始化。
#include <stdio.h>
int main()
{
int a = 0;
const int c = a;
int* p = (int*)&c; //类型不匹配会有警告
printf("Begin...\n");
*p = 5;
printf("c = %d\n", c);
printf("*p = %d\n", *p);
printf("End...\n");
return 0;
}
操作:
1) g++ test1.cpp -o test1.out编译正常,打印结果:
Begin...
c = 5
*p = 5
End...
4)C++中的const常量类似于宏定义
- constint c = 5; 约等于#define c 5
const是被编译器处理,具有作用域和类型检查;预编译只是文本替换
5)C++中的const常量与宏定义不同
- const常量是由编译器处理
- 编译器对const常量进行类型检查和作用域检查
- 宏定义由预处理器处理,单纯的文本替换
编程实验
const与宏
3-2.cpp
#include <stdio.h>
void f()
{
#define a 3
const int b = 4;
}
void g()
{
printf("a = %d\n", a);
}
int main()
{
const int A = 1;
const int B = 2;
int array[A + B] = {0}; //
int i = 0;
for(i=0; i<(A + B); i++)
{
printf("array[%d] = %d\n", i, array[i]);
}
f();
g();
return 0;
}
操作:
1) g++ 3-2.cpp -o 3-2.out编译正常,运行结果:
array[0] = 0
array[1] = 0
array[2] = 0
a = 3 //宏定义是全局的,不受作用域影响
gcc 3-2.c -o 3-2.out编译错误:
3-2.c:19:2: error: variable-sized object may not be initialized
int array[A + b] = {0};
错误:大小可变的对象可能没有初始化。
省略其他错误
小结
1)与C语言不同,C++中的const不是只读变量
2)C++中的const是一个真正意义上的常量
3)C++编译器可能会为const常量分配空间
4)C++完全兼容C语言中const常量的语法特性