Sign in to follow this  

Register keyword

This topic is 4659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this