const volatile,寄存器volatile,C ++中的静态volatile [英] const volatile, register volatile, static volatile in C++

查看:216
本文介绍了const volatile,寄存器volatile,C ++中的静态volatile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 volatile 关键字与注册 const 静态的不同用法,关键字。我不知道是什么影响,所以我想:

 注册volatile int T = 10; 

建议编译器将T存储在寄存器中,T的值可以从外部OS,硬件,另一个线程)

  const volatile int T = 10; 

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

  static volatile int T = 10; 

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



思想是正确的,任何经验丰富的C ++开发人员都可以给出一个例子,上面的例子可能在现实世界的应用程序中使用,或者是非常罕见的。

解决方案

pre> register volatile int T = 10;

volatile 限定符意味着编译器不能应用优化或重新访问 T ,而寄存器是提示编译器 T 将被大量使用。如果采用 T 的地址,编译器将忽略提示。请注意,寄存器已弃用,但仍在使用。



实用用法:



我从来没有使用过它,从来没有感觉到它的需要,






  const volatile int T = 10; 

const 限定符表示 T 无法通过代码修改。如果尝试这样做,编译器将提供诊断。 volatile 仍然意味着与情况1相同。编译器无法优化或重新排序对 T 的访问。



实用用法:




  • 以只读模式存取共享内存。

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






  static volatile int T = 10; 

static c> T 静态存储持续时间(C ++ 11§3.7)和内部链接,而 volatile 仍然管理优化和重新排序。



实用用法:




  • volatile 相同,除了您需要对象具有静态存储时长,并且无法从其他翻译单位访问。


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;

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;

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

static volatile int T=10;

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.

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 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.

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, while volatile 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.

这篇关于const volatile,寄存器volatile,C ++中的静态volatile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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