Volatile关键字

 

Volatile关键字

·         The compiler assumes that, at any point in the program, avolatile variable can be accessed by an unknown process that uses or modifies its value. Therefore, regardless of the optimizations specified on the command line, the code for each assignment to or reference of avolatile variable must be generated even if it appears to have no effect.

If volatile is used alone, int is assumed. Thevolatile type specifier can be used to provide reliable access to special memory locations. Usevolatile with data objects that may be accessed or altered by signal handlers, by concurrently executing programs, or by special hardware such as memory-mapped I/O control registers. You can declare a variable asvolatile for its lifetime, or you can cast a single reference to bevolatile.

 

·         直接存取原始内存地址

           能够防止编译器优化。

            编译器每次用到这个变量的值的时候都会直接从内存中读取而不是读取寄存器中的备份。通过反汇编可能会发现一些问题(待续).http://www.dutor.net/index.php/2010/08/volatile-const/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值