关键字
来源:互联网 发布:apache 代理转发 编辑:程序博客网 时间:2024/05/21 11:05
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num = 10;
int * p = (int *)#
* p = 20;
printf("%d\n", num);
system("pause");
return 0;
#include <stdlib.h>
int main()
{
int num = 10;
int * p = (int *)#
* p = 20;
printf("%d\n", num);
system("pause");
return 0;
}
输出20
在第5行前添加const,如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
const int num = 10;
int * p = (int *)#
* p = 20;
printf("%d\n", num);
system("pause");
return 0;
}
输出10
因为添加const之后,num的值10自动存储到寄存器,虽然这里第6,7行绕过const将10换成20,但是编译器会自动认为const令其不变,自动去寄存器中找寻num值。
在const前再添加关键字volatile,如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
volatile const int num = 10;
int * p = (int *)#
* p = 20;
printf("%d\n", num);
system("pause");
return 0;
}
输出20
因为volatile能够“保证内存可见性”,它会令编译器去内存块挖掘数据,而不是去寄存器找数据。
阅读全文
0 0
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- 关键字
- Oracle数据恢复顾问(Data Recovery Advisor)
- scala学习笔记(四)
- Eclipse输入不了中文问题
- (一)托管项目到GitHub
- Pycharm2017.1.4连接远程服务器调试代码
- 关键字
- Matplotlib 中文字体设置
- 排序算法实现(下)
- JS设置cookie、读取cookie、删除cookie
- vue里面删除数据
- beaglebone black 怎么使能pwm
- java的IO之字节流
- elasticsearch2.3.2服务搭建、管理及实时同步mysql数据
- redis集群