什么时候在C ++中使用const volatile,注册volatile,static volatile? [英] When would I use const volatile, register volatile, static volatile in C++?

查看:70
本文介绍了什么时候在C ++中使用const volatile,注册volatile,static volatile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 volatile 关键字与 register const static 结合使用的不同用法关键字。我不确定会产生什么影响,所以我认为:

I am wondering about the different uses of the volatile keyword in combination with register, const and static keywords. I am not sure what are the effects, so I think:

register volatile int T=10;

建议编译器将T存储在寄存器中,并且T的值可以在外部(操作系统,硬件,另一个线程)

Suggest the compiler to store T in a register and the value of T can be modified from somewhere outside (OS, hardware, another thread)

const volatile int T=10;

程序本身不能修改T,但是可以在代码之外的某个地方修改T。

The program itself can not modify T, but T can be modified frow somewhere outside the code.

static volatile int T=10;

如果T是类的数据成员,则意味着该类的所有对象都具有相同的对象T和T的值可以从外部修改。如果T是文件中的全局变量,则其他文件(属于项目的一部分)中的源代码无法访问T,但是可以从外部某处访问T。如果T是函数中的局部变量,则一旦被初始化,它就会一直保留在内存中,直到程序结束,并且可以从外部进行修改。

If T is a data member of a class it means that all the objects of the class have the same value for T and T can be modified from somewhere outside. If T is a global variable in a file, the source code in other files (that are part of the project) cannot access T, but T can be accessed from somewhere outside. If T is a local variable in a function,once it has been initialized remains in the memory until the end of the program and can be modified from somewhere outside.

想法正确,并且任何经验丰富的C ++开发人员都可以举一个示例,说明上述示例可以在实际应用中使用还是很少见?

Are my thoughts correct and can any experienced C++ developer give an example where the above maybe used in real-world applications or it is very rare?

推荐答案

register volatile int T=10;

volatile 限定词意味着编译器无法应用优化或重新排序对 T 的访问权限,而寄存器向编译器提示 T 将被大量使用。如果使用地址 T ,则编译器将忽略该提示。请注意,已弃用 register 但仍在使用。

volatile qualifier means that the compiler cannot apply optimizations or reorder access to T, While register 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.

实际用法:

我从没使用过它,从来没有感觉到它的需要,也不能真正地思考

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 限定词表示 T 不能通过代码进行修改。如果尝试这样做,编译器将提供诊断信息。 volatile 的含义仍与情况1相同。编译器无法优化或重新排序对 T 的访问。

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.

实际用法:


  • 以只读模式访问共享内存。

  • 以只读模式访问硬件寄存器。

static volatile int T=10;

静态存储限定符给出 T 静态存储持续时间(C ++ 11§3.7)和内部链接,而 volatile 仍控制优化和重新排序。

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

实用用法:


  • volatile 相同,只是需要对象具有固定的存储期限,并且其他翻译单位无法访问。

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

这篇关于什么时候在C ++中使用const volatile,注册volatile,static volatile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆