cpp中的volatile关键字

本文探讨了在编程中如何使用volatile关键字来声明那些可能被外部进程改变的对象。这些对象的值不受程序直接控制,例如由系统时钟更新的变量。volatile指示编译器不要对这类对象进行优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Programs that deal directly with hardware often have data elements whose value is controlled by processes outside the direct control of the program itself. For example, a program might contain a variable updated by the system clock. An object should be declared volatile when its value might be changed in ways outside either the control or detection of the compiler. The volatile keyword is a directive to the compiler that it should not perform optimizations on such objects.

 

volatile int v; // v is a volatile int int *volatile vip; // vip is a volatile pointer to int volatile int *ivp; // ivp is a pointer to volatile int // vivp is a volatile pointer to volatile int volatile int *volatile vivp; int *ip = &v; // error: must use pointer to volatile *ivp = &v; // ok: ivp is pointer to volatile vivp = &v; // ok: vivp is volatile pointer to volatile

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值