简体   繁体   English

c99语义中的存储类说明符“注册”

[英]storage-class specifier 'register' in c99 semantics

I'm a foreigner reading C99, while a sentence (in 6.7.1) makes me confused: 我是读C99的外国人,而一个句子(在6.7.1中)让我感到困惑:

  • '(A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible.) The extent to which such suggestions are effective is implementation-defined. “(使用存储类说明符寄存器声明对象的标识符表明,应尽可能快地访问该对象。) 此类建议有效的程度是实现定义的。 ' '

How should I parse the second sentence : 我应该如何解析第二句话:

  • The extent to, which such suggestions are effective , is implementation-defined. 这些建议有效的程度是实施定义的。
  • The extent, to which such suggestions are effective , is implementation-defined. 这些建议有效的程度是实施定义的。

which one is better? 哪一个更好?

Does that means an implementation has full powers to decide how to deal with register , even with a termination of translation? 这是否意味着即使在终止翻译的情况下,实现也具有决定如何处理寄存器的全部能力?

Thanks. 谢谢。

Don't mix up register variables with registers of the CPU. 不要将register变量与CPU的寄存器混淆。 These are not the same. 这些不一样。

register has the purpose to allow for optimizations. register的目的是为了进行优化。 Taking the address of a such a variable is forbidden and as a consequence such a variable can never alias, the compiler always masters its latest value. 禁止使用此类变量的地址,因此此类变量永远不能混叠,因此编译器始终掌握其最新值。 Thus it can realize such a variable easily as a CPU register or an assembler immediate, for example. 因此,例如,它可以轻松实现诸如CPU寄存器或汇编器之类的变量。

As a particular subcase there are const qualified register variables that can't be altered by the program directly nor behind the scenes by some other code that would access it through a pointer. 作为一个特殊的子情况,存在const限定的register变量,这些变量不能由程序直接更改,也不能由其他可通过指针访问它的代码在后台更改。 Only such variables can easily be guaranteed to be constant through out the whole program execution. 在整个程序执行过程中,只有这样的变量才能轻松保证是恒定的。

Register is an old keyword. 注册是一个旧关键字。 Basically it is a hint to an optimizer telling that it is better to use register for storing this variable. 基本上,这是对优化器的提示,告诉您最好使用寄存器来存储此变量。 The pharse that you are mentioning means that compiler can treat this hint in any way it wants. 您提到的术语意味着编译器可以按其希望的任何方式处理此提示。 In our days of optimizing compilers, its meaning and value for 99% is nothing. 在我们优化编译器的时代,其意义和99%的价值无济于事。

No -- the extent to which the suggestions are effective, is implementation defined, but the effect of the code as a whole is not. 否-建​​议的有效程度已在实现中定义,但整个代码的效果尚不明确。

To put it slightly differently, when/if you put register into your code (where allowed), the compiler can completely ignore it -- and I feel obliged to add that nearly all reasonably modern compilers will . 换句话说,当/如果将register放入代码中(允许的话),编译器可以完全忽略它-我觉得有必要补充一点,几乎所有合理的现代编译器都可以

Nonetheless, the mere presence of register won't prevent the code from compiling, unless you try to apply it somewhere it's not allowed (eg, to a global variable). 尽管如此,仅register存在不会阻止代码的编译,除非您尝试将其应用于不允许的地方(例如,应用于全局变量)。

Bottom line: don't use it, but don't worry about removing it from code that already has it either. 底线:不要使用它,但也不必担心从已经具有它的代码中删除它。 It's a waste of time and effort, but with reasonably modern compilers a completely harmless one. 这是浪费时间和精力,但是对于相当现代的编译器来说,这是完全无害的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM