• <bdo id='e2dB9'></bdo><ul id='e2dB9'></ul>
    <tfoot id='e2dB9'></tfoot>

    <small id='e2dB9'></small><noframes id='e2dB9'>

    <legend id='e2dB9'><style id='e2dB9'><dir id='e2dB9'><q id='e2dB9'></q></dir></style></legend>
    <i id='e2dB9'><tr id='e2dB9'><dt id='e2dB9'><q id='e2dB9'><span id='e2dB9'><b id='e2dB9'><form id='e2dB9'><ins id='e2dB9'></ins><ul id='e2dB9'></ul><sub id='e2dB9'></sub></form><legend id='e2dB9'></legend><bdo id='e2dB9'><pre id='e2dB9'><center id='e2dB9'></center></pre></bdo></b><th id='e2dB9'></th></span></q></dt></tr></i><div id='e2dB9'><tfoot id='e2dB9'></tfoot><dl id='e2dB9'><fieldset id='e2dB9'></fieldset></dl></div>

        我什么时候会在 C++ 中使用 const volatile、register volatile、static vola

        时间:2023-12-02

        <small id='FfzlI'></small><noframes id='FfzlI'>

          • <tfoot id='FfzlI'></tfoot>
                <bdo id='FfzlI'></bdo><ul id='FfzlI'></ul>
                <i id='FfzlI'><tr id='FfzlI'><dt id='FfzlI'><q id='FfzlI'><span id='FfzlI'><b id='FfzlI'><form id='FfzlI'><ins id='FfzlI'></ins><ul id='FfzlI'></ul><sub id='FfzlI'></sub></form><legend id='FfzlI'></legend><bdo id='FfzlI'><pre id='FfzlI'><center id='FfzlI'></center></pre></bdo></b><th id='FfzlI'></th></span></q></dt></tr></i><div id='FfzlI'><tfoot id='FfzlI'></tfoot><dl id='FfzlI'><fieldset id='FfzlI'></fieldset></dl></div>

                    <tbody id='FfzlI'></tbody>
                • <legend id='FfzlI'><style id='FfzlI'><dir id='FfzlI'><q id='FfzlI'></q></dir></style></legend>

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

                  问题描述

                  我想知道 volatile 关键字与 registerconststatic 的不同用法关键词.我不确定有什么影响,所以我认为:

                  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 应用优化或重新排序访问,而 register 是对编译器的提示 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;
                  

                  static 存储限定符给出 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、register volatile、static volatile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么“静态"关键字在 C 和 C++ 中有这么多含义? 下一篇:是否可以将友元函数声明为静态函数?

                  相关文章

                  1. <tfoot id='ctoYV'></tfoot>

                    <small id='ctoYV'></small><noframes id='ctoYV'>

                      <bdo id='ctoYV'></bdo><ul id='ctoYV'></ul>

                      <legend id='ctoYV'><style id='ctoYV'><dir id='ctoYV'><q id='ctoYV'></q></dir></style></legend>
                    1. <i id='ctoYV'><tr id='ctoYV'><dt id='ctoYV'><q id='ctoYV'><span id='ctoYV'><b id='ctoYV'><form id='ctoYV'><ins id='ctoYV'></ins><ul id='ctoYV'></ul><sub id='ctoYV'></sub></form><legend id='ctoYV'></legend><bdo id='ctoYV'><pre id='ctoYV'><center id='ctoYV'></center></pre></bdo></b><th id='ctoYV'></th></span></q></dt></tr></i><div id='ctoYV'><tfoot id='ctoYV'></tfoot><dl id='ctoYV'><fieldset id='ctoYV'></fieldset></dl></div>