Why can't we always use the register storage class in C?

I read in the book that whenever we declare a variable with a storage class as a register, it will be stored in a register based on its accessibility. If the register is not available, it will be assigned the default storage type 'auto'.

Whenever we declare a variable without explicitly mentioning any storage class, the default storage type assigned to it is "automatic".

So my question is, why not declare that each variable belongs to a "register" storage class - if there is no register, it will be treated as an "automatic" class in and of itself, and luckily if registers are available. then they will be stored in one. I understand that we can no longer use the and operator, but what if I do not work with pointers and addresses? Can I declare these variables with the "register" storage class? Because it seems like bad practice.

Edit: I searched the web, but "address not available" is the only point mentioned. Why can't the rest of the variables be declared with "register".

+3


source to share


2 answers


You cannot make all of your variables register

, because the C (and C ++) language spec (s) explicitly forbids accepting a variable address register

.

However, the qualifier register

has no role
today optimizes compilers like GCC or Clang / LLVM , and those compilers will be happy and free to use machine register for unqualified variables register

, or even store a variable in memory (not machine register) qualified as register

. Essentially, the compiler ignores the qualifierregister

(except that it forbids accepting its address). It contains sophisticated register allocation algorithms and heuristics . This variable can remain in the machine register for some parts of the functional code and fit into memory for other parts.

From an optimization standpoint, current compilers treat qualified variables in the same way auto

and register

(hence the classifier is register

useless other than disallowing the address-operator).



Note that the processor's cache is much more important today than the processor registers . If you want to tweak your C code for performance (which is often a bad idea since the compiler is doing better than you can), you better take care of the cache issues (see this ).

AFAIK, future versions of the C and C ++ languages ​​will officially deprecate the qualifier register

(as well as for the qualifier auto

), and it may be that future language specifications will reuse this keyword for other purposes (e.g. C ++ 11 reused auto

). Thus, using register

in your original code is probably a mistake
, as it could make your code harder to port to future versions of C or C ++.

+6


source


The "register" keyword is just a hint to the compiler, which you think should be processed faster than others, if possible. As a side effect, variable address is not allowed and register arrays are undefined.

Any modern compiler will use registers as much as possible anyway, so this keyword is no longer needed. The compiler is also smarter than you: it can register x in one part and y in another part of the program. Or use registers for two of the five structure fields. Anything you can't even express with the register keyword.



The only time registering might be completely pointless is when you have a variable that looks like it is being used much less than others, but you know better. Even then, this is a very big "can".

+3


source







All Articles