address of register variable in C and C++

前端 未结 8 1892
野趣味
野趣味 2021-01-06 03:00

I know the concept of register variable and it\'s use cases but there are few questions in my mind based on what I have tried.

  1. I cannot access the address

8条回答
  •  醉酒成梦
    2021-01-06 03:29

    C and C++ are different languages.

    • In C, you cannot take the address of a variable with register storage. Cf. C11 6.7.1/6:

      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.

      Footnote: The implementation may treat any register declaration simply as an auto declaration. [...]

    • In C++, register is a deprecated, meaningless keyword that has no effect (except perhaps serve as a compiler hint), and variables declared as register still just have automatic storage. In particular, C++ doesn't have a "register" storage class. (It just has the storage class specifier, inherited from C.) Cf. C++11, 7.1.1/3:

      A register specifier is a hint to the implementation that the variable so declared will be heavily used. [ Note: The hint can be ignored and in most implementations it will be ignored if the address of the variable is taken. This use is deprecated [...]

    Even in C nothing is actually guaranteed about how register storage is implemented (implementations are free to treat register as auto), but the language rules apply regardless.

提交回复
热议问题