views:

1201

answers:

3

I'm reading through K&R and came to the small section on register variables, and was wondering if people here have some good examples of this put into practice.

From section 4.7 in K&R:

The register declaration looks like
register int x;
register char c;

To be clear, I'm just hoping to see some cool code samples. I (am pretty sure that I) understand the subject matter so don't feel the need to type up a verbose explanation (unless you want to).

+24  A: 

There is no good example of register usage when using modern compilers (read: last 10+ years) because it almost never does any good and can do some bad. When you use register, you are telling the compiler "I know how to optimize my code better than you do" which is almost never the case. One of three things can happen when you use register:

  • The compiler ignores it, this is most likely. In this case the only harm is that you cannot take the address of the variable in the code.
  • The compiler honors your request and as a result the code runs slower.
  • The compiler honors your request and the code runs faster, this is the least likely scenario.

Even if one compiler produces better code when you use register, there is no reason to believe another will do the same. If you have some critical code that the compiler is not optimizing well enough your best bet is probably to use assembler for that part anyway but of course do the appropriate profiling to verify the generated code is really a problem first.

Robert Gamble
Thanks for the insight, it was very informative.
Kyle Walsh
"Three things can happen, and two are bad"? Where have I heard that before... ;-)
Steve Jessop
@onebyone: I don't get it.
Robert Gamble
Remember the compiler is perfectly free to ignore your suggestion - there is nothing in the standard to say it has to put a register variable in a register.
Martin Beckett
could also be the case you write a compiler backend and you say "register" for all locals in a function will make a stackless function. that would be quite useful i think.
Johannes Schaub - litb
@mgb: did you actually read my answer? Ignoring the keyword is the first case I discusss and I also state it is the most likely thing to happen...
Robert Gamble
although in that case __attribute__((register_function)) or the like would be better i think
Johannes Schaub - litb
+4  A: 

In general i agree with Robert, but as any good rule this one has exceptions as well.
If you working on deeply embedded system you might know better than compiler how to optimize the code for your specific application on your specific hardware architecture.

But in 99% of cases Roberts explanation good for embedded word as well.

Ilya
This looks pretty much like what Robert said, in all honesty.
Jonathan Leffler
Actually you kind of right. I reread the post and the last paragraph is clarifying what i wanted to clarify... Next time i will read better
Ilya
A: 

Another common case is when implementing low-level interpreters. Keeping some state in registers, eg. virtual machine stack pointer, can reduce the memory access significantly and speed up you code.

See vmgen — a generator of efficient virtual machine interpreters for an example of the optimization (5.2 Top of stack caching).

Hrvoje Prgeša