const volatile, register volatile, static volatile in C++

来源: http://stackoverflow.com/questions/16259939/const-volatile-register-volatile-static-volatile-in-c


register volatile int T=10;

volatile qualifier means that the compiler cannot apply optimizations or reorder access to T, Whileregister is a hint to the compiler that T will be heavily used. If address of T is taken, the hint is simply ignored by the compiler. Note that register is deprecated but still used.

Practical Usage:

I have never used it never felt the need for it and can't really think of any right now.


const volatile int T=10;

const qualifier means that the T cannot be modified through code. If you attempt to do so the compiler will provide a diagnostic. volatile still means the same as in case 1. The compiler cannot optimize or reorder access to T.

Practical Usage:

  • Accessing shared memory in read-only mode.
  • Accessing hardware registers in read-only mode.

static volatile int T=10;

static storage qualifier gives T static storage duration (C++11 §3.7) and internal linkage, whilevolatile still governs the optimization and reordering.

Practical Usage:

  • Same as volatile except that you need the object to have static storage duration and to be inaccessible from other translation units.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值