As I know that in C we could use the keyword "register" to suggest to the compiler that the variable should be stored in the CPU register. Isn't it true that all variables that involved in CPU instructions will be eventually stored in CPU registers for execution?
4 Answers
The register keyword is a way of telling the compiler that the variable is heavily used. It's true that values must usually be loaded temporarily into registers to perform calculations on them. The name comes from the idea that a compiler might keep the variable in a register for the entire duration that it is in scope, rather than only temporarily when it is being used in a calculation.
The keyword is obsolete for the purpose of optimisation, since modern compilers can determine when a variable is heavily used (and when it does not have its address taken) without help from the programmer.
- 233,326
- 40
- 323
- 462
-
-
The compiler determines the duration for you - it may be less or more than the source code block / scope after optimizations. Less if the value is not used in the latter part of the scope and the memory/register can be effectively used for something else, more if the value is returned out of the current block in some way and should be kept alive for efficiency reasons outside of the current scope. – Joris Timmermans Sep 30 '10 at 08:06
-
-1: the keyword is not obsolete. Unfortunately nowadays it has not much to do with registers of the target machine, but its only meaning is that you are not permitted to take an address of that variable. This can be a precious optimization hint and can also be important in terms of security. It hinders that you inadvertently may expose the address of a stack variable. – Jens Gustedt Sep 30 '10 at 08:30
-
I think it is fine for me Jens, I don't really care if people are still using it nowadays. As long as I get the point of the keyword – woongiap Sep 30 '10 at 08:41
-
1@Jens Gustedt: It is not a "precious" optimisation hint, since the modern compiler is perfectly capable of determining that the address of a variable is never taken (compilers are no longer single-pass). In fact any compiler that supports a register-based calling convention (eg. any x86-64 compiler) likely already does this analysis for function parameters, to avoid having to unnecessarily spill them to the stack. Decent compilers also warn about returning the address of an automatic variable. – caf Sep 30 '10 at 12:22
-
1@caf: the question for me is not what the compiler is capable to do, the thing is that the `register` keyword binds the programmer to a certain behavior. In complicated code he might not be capable of keeping track whether or not he takes an address somewhere, so the compiler will tell him. And BTW, you may see the -1 for expressing an opinion and not marking it as such. If you like, see my post at https://gustedt.wordpress.com/2010/08/17/a-common-misconsception-the-register-keyword/ – Jens Gustedt Sep 30 '10 at 13:25
-
@Jens Gustedt: I agree that it can be used to ensure that taking the address of a variable is an error for which a diagnostic must be issued, but it *is* obsolete for the purposes of optimisation (which is certainly the original intention of the keyword). I have updated the answer accordingly. – caf Sep 30 '10 at 13:40
You should not use that register keyword. It is an antique relic, maintained for backward compatibility. Most compilers will ignore it (by default).
There could be exceptions but they are very rare, consult your compiler manual.
Isn't it true that all variables that involved in CPU instructions will be eventually stored in CPU registers for execution?
Yes, that is true. But CPU registers are limited so variables are usually LOAD/STOREd from 'normal' memory and live in a register only briefly. The register keyword is (was) a way of indicating high priority variables that should occupy a register longer. Like the i in for(i = 0; ...).
- 263,252
- 30
- 330
- 514
-
3just because I shouldn't use it doesn't mean I shouldn't ask about it – woongiap Sep 30 '10 at 07:56
-
@woong: you are absolutely right. But my answer starts with the most important thing to know about `register`. – H H Sep 30 '10 at 08:04
-
@Henk: thanks, I was just curious about the definition when reading about it on the manual, I am not gonna use it, which answer so far do you think is best? – woongiap Sep 30 '10 at 08:09
-
@Henk, thanks for the correct answer, but I guess caf was the first to provide correct answer – woongiap Sep 30 '10 at 08:21
-
@Jens: And why don't you write your own answer? I'd like to know how standard this is. – H H Sep 30 '10 at 08:48
-
@Henk: The answer had already been accepted when I came to it. I wrote some things up about this here: https://gustedt.wordpress.com/2010/08/17/a-common-misconsception-the-register-keyword/ – Jens Gustedt Sep 30 '10 at 08:51
-
@Henk Holterman: The C99 standard section 6.7.1 footnote 101 prohibits taking the address of a register variable. Where Jens is wrong is in suggesting that you need a flag to say "don't take the address of this variable". If you need its address, you need its address. – JeremyP Sep 30 '10 at 09:06
-
@Jeremy, "If you need its address, you need its address": yes, it would only apply to local vars and that makes the usefulness of such a 'flag' very doubtful. – H H Sep 30 '10 at 09:29
-
-1: The register keyword has no effect, this is modern myth, just like the assumption that static const variables are always optimized out in C. Take a look at Jen's comments. – Matt Joiner Oct 02 '10 at 09:48
In old times, compilers weren't that smart as they are today. It was a hint from the programmer to the compiler that this variable should be stored in a register to allow fast access/modification. Today, almost any decent compiler implements clever registers allocation algorithms that beats the mind of humans.
- 94,250
- 39
- 176
- 234
Most variables will be loaded into registers for a short while...as long as necessary to do what needs to be done with them. The register keyword hints that they should be kept there.
Compilers' optimization has gotten so much better, though, that the register keyword isn't very helpful. In fact, if your compiler respects it at all (and many don't), it could even mess you up (by tying the compiler's hands, making certain optimizations impossible). So it's a pretty bad idea these days.
- 84,970
- 20
- 145
- 172
-
thanks for answering, just that caf was the first to provide a correct answer – woongiap Sep 30 '10 at 08:22