Explore the differences between register keywords in c and c++

  • 2020-04-02 01:45:37
  • OfStack

In c + + :

(1) register The keyword cannot define a variable globally, or it will be prompted as an incorrect storage class.

(2) register When a keyword is declared in a local scope, the & operator is used to fetch the address, and once the address operator is used, the defined variable is forced to be stored in memory.

In c:

(1) register The keyword can define variables globally, and when the & operator is used on its variables, it simply warns of "bad storage classes".

(2) register Keywords can be declared in a local scope, but you cannot use the & operator on them. Otherwise the compilation will not pass.

It is recommended that you do not use the register keyword to define global variables, because global variables have a life cycle that starts with the execution of the program and ends at the end of the program, and register variables may be stored in registers on the CPU, which is a pretty bad move if they are occupied throughout the life of the program.

Here's an excerpt from someone else on the Internet:

C and C++ handle a difference in the register keyword

C++ is not fully compatible with C, as was the case with sizeof('a') last time. Today I stumbled across another difference while coding:

Variables decorated with the register keyword cannot be addressed with the & operator in c, which is my experience. Because if the compiler takes the programmer's advice and stores the variable in a register, it does not have a virtual address. But in C++, variables decorated with registers can be addressed with the & operator, as I discovered in a piece of code. If the program explicitly takes the address of a register variable, the compiler must define the variable in memory rather than as a register variable.

I have been confirmed in the C99 (ISO/IEC 9899:1999) and ISO C++ (ISO/IEC 14882:2003) standards that the C and C++ standards do have different clear definitions of how register encounters & are handled. But why define it that way? I can only guess between the lines of the standard. K&R C1 how to describe the register I has not been verified, K&R C2 (ANSI C) illustrates the "register variables are to be placed in the machine registers... But compilers are free to ignore the advice." But in C99 and ISO c + +, the phrase respectively became: "suggests that the access to the object be as fast as possible", "a hint to the implementation that the object so declared will be heavily, informs", no longer mention "machine registers". So while the register keyword has historically emphasized saving variables to registers as much as possible, the register keyword now de-emphasizes the specific approach and simply recommends that the compiler optimize access to the variable in all possible ways (though many compilers ignore this keyword and adopt their own optimization strategy). C99 may not allow fetch operations to maintain compatibility with K&R C; C++ may have relaxed this restriction because it had no historical baggage. It's just a guess, but a more accurate answer from a friend who wants to know the inside story.


Related articles: