tags:

views:

142

answers:

5

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?

A: 

This related question might help: "register" keyword in C?

Simon Whitaker
that one is not really what I wanted to ask, but thanks
woongiap
+1  A: 

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.

AraK
+4  A: 

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; ...).

Henk Holterman
just because I shouldn't use it doesn't mean I shouldn't ask about it
woongiap
@woong: you are absolutely right. But my answer starts with the most important thing to know about `register`.
Henk Holterman
@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
@Henk, thanks for the correct answer, but I guess caf was the first to provide correct answer
woongiap
-1 for a wrong answer. See my comment at caf's answer.
Jens Gustedt
@Jens: And why don't you write your own answer? I'd like to know how standard this is.
Henk Holterman
@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
@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
@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.
Henk Holterman
-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
+5  A: 

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.

caf
how to determine the "duration" of the scope? a block in C?
woongiap
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.
MadKeithV
-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
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
+1: to counter the unreasonable -1.
JeremyP
@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
@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
@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
@caf: ok, removed.
Jens Gustedt
+1  A: 

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.

cHao
thanks for answering, just that caf was the first to provide a correct answer
woongiap