tags:

views:

1657

answers:

9

What does the register keyword do in C? I have read that it is used for optimizing but is not clearly defined in any standard. Is it still relevant and if so, when would you use it?

+8  A: 

It tells the compiler to try to use a CPU register, instead of RAM, to store the variable. Registers are in the CPU and much faster to access than RAM. But it's only a suggestion to the compiler, and it may not follow through.

Andrew Barnett
+7  A: 

It hasn't been relevant for at least 15 years as optimizers make better decisions about this than you can. Even when it was relevant, it made a lot more sense on a CPU architecture with a lot of registers, like SPARC or M68000 than it did on Intel with its paucity of registers, most of which are reserved by the compiler for its own purposes.

Paul Tomblin
+29  A: 

It's a hint to the compiler that the variable will be heavily used and that you recommend it be kept in a processor register if possible.

Most modern compilers do that automatically, and are better at picking them than us humans. :-)

Brian Knoblauch
Well, I experimented with register to get my ACM submissions tweaked, and sometimes it really helped. But you really have to be carful, because poor choices degrade performance.
ypnos
A good reason _not_ to use 'register': you can't take the address of a variable declared 'register'
Adam Rosenfield
Note that some/many compilers will completely ignore the register keyword (which is perfectly legal).
Euro Micelli
ypnos: Actually the speed of a solution for ACM ICPC problems depends much more on the algorithm choice than on such micro-optimizations. The 5-second time limit is usually enough for a correct solution, especially when using C instead of Java.
Joey
Also note that the effect of "register" (if the compiler chooses to respect it) will vary a lot between processor architectures and models. So even if "register" makes your program faster on your Pentium IV, it may make it slower on a Xeon, or an AMD processor, or a Sparc... . So it's really more like a last-ditch optimization.
sleske
@Euro: You probably know this, but just to be explicit, the compiler is required to prevent the address of a `register` variable from being taken; this is the *only* mandatory effect of the `register` keyword. Even this is sufficient to improve optimizations, because it becomes trivial to tell that the variable can only be modified within this function.
Dale Hagglund
@Dale Hagglund: A nice observation and summary.
Matt Joiner
+2  A: 

Register would notify the compiler that the coder believed this variable would be written/read enough to justify its storage in one of the few registers available for variable use. Reading/writing from registers is usually faster and can require a smaller op-code set.

Nowadays, this isn't very useful, as most compilers' optimizers are better than you at determining whether a register should be used for that variable, and for how long.

Bill James
+2  A: 

On supported C compilers it tries to optimize the code so that variable's value is held in an actual processor register.

Dana Holt
+3  A: 

You are messing with the compiler's sophisticated graph-coloring algorithm. This is used for register allocation. Well, mostly. It acts as a hint to the compiler -- that's true. But not ignored in its entirety since you are not allowed to take the address of a register variable (remember the compiler, now on your mercy, will try to act differently). Which in a way is telling you not to use it.

The keyword was used long, long back. When there were only so few registers that could count them all using your index finger.

But, as I said, deprecated doesn't mean you cannot use it.

dirkgently
Some of the older hardware had more registers than the modern Intel machines. Register counts have nothing to do with age and everything to do with CPU architecture.
JUST MY correct OPINION
+5  A: 

I'm surprised that nobody mentioned that you cannot take an address of register variable, even if compiler decides to keep variable in memory rather than in register.

So using register you win nothing (anyway compiler will decide for itself where to put the variable) and loose the & operator - no reason to use it.

qrdl
+1  A: 

Actually, register tells the compiler that the variable does not alias with anything else in the program (not even char's).

That can be exploited by modern compilers in a variety of situations, and can help the compiler quite a bit in complex code - in simple code the compilers can figure this out on their own.

Otherwise, it serves no purpose and is not used for register allocation. It does not usually incur performance degradation to specify it, as long as your compiler is modern enough.

Rupert
A: 

"register"...key word is particulary use some machine language..to store some data in a particular register....for more informatio..go through.."LET US C"(Y.K),, ..............sahanaj rahaman & mehedi hasan.(dgp).

sahanaj rahaman