Register keyword
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
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.
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:
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);}
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.
This would be a cool use for auto, though.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement