Register keyword

Started by
5 comments, last by smart_idiot 19 years, 1 month ago
I was just in my computer architecture class, and my prof informs me that C has a 'register' keyword. According to him, it clues the compiler in that the variable should be stored in a register, if possible. Does this actually provide a decent enough performance boost to justify me trying it, and do all C/C++ compilers provide it (is it part of the standard)? Thanks
Advertisement
Yup, it's part of the standard.

However, my understanding is (and I'm quite happy to be corrected on this) - your compiler is probably better at working out what should be stored in a register than you are.

Jim.
That's right. register was only ever a hint to the compiler, and most modern compilers ignore it.
Again, yes, it's best to let the compiler decide. I've seen a lot of old C code using it, particularly for things like software blitters, where speed is important.
Assuming you prof. didn't say how to use it, it's just a modifier like unsigned. Some example code:
void DoSomething(void){register int i;   for(i=0; i<100000; ++i)      DoSomethingElse(i);}
Yeah, like others said, most compilers just ignore you entirely.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Ravuya
(is it part of the standard)?

It is indeed part of the standard.

Using the register keyword can starve the compiler of registers, although with modern compilers it shouldn't make much of a difference. The this keyword (C++) is almost always stored in a register.

Quote:Most compilers these days (like gcc) are so smart that suggesting registers could actually make your program slower.


Quote:Note that TIGCC will automatically store often used variables in CPU registers when the optimization is turned on, but the keyword register will force storing in registers even if the optimization is turned off. However, the request for storing data in registers may be denied, if the compiler concludes that there is not enough free registers for use at this place.


I just got this off the MSDN website: MSDN Home > MSDN Library > Embedded Operating System Development > C++ Language Reference > Specifiers > Storage-Class Specifiers.
Quote:
Microsoft Specific

The compiler does not accept user requests for register variables; instead, it makes its own register choices when global register-allocation optimization (/Oe option) is on. However, all other semantics associated with the register keyword are honored.
auto and register could probably vanish from the language and nobody would notice.

This would be a cool use for auto, though.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement