Archived

This topic is now archived and is closed to further replies.

Register Keyword

This topic is 6025 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

Is the register keyword becoming obsolete? I was looking through the compiler options for Borland C++ and found that the defualt for the register keyword was automatic. I assume it means it will figure out when to use it. Also when exactly do you use(or should use) the register keyword? Even though its not in the title of the topic how obsolete is C? Are most people still writing C++ code under certain C restrictions or is just about everything C++. "Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Share this post


Link to post
Share on other sites
register is very handy when working on embedded systems that link memory locations to hardware lines:
register byte *ctrl = ADDR_OUTPUT_HDR;
*ctrl &= ~BIT_NRESET_PIN;
*ctrl |= BIT_NRESET_PIN;

If not for the register keyword, the compiler may compile-out those lines since *ctrl is in its original state after the calls. But register assures that does not happen, which allows me to lower the NRESET output pin for a couple of clock cycles.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
register means that variable will be stored in the cpu directly, not in memory. The result is that that particular variable will be *very* fast, but the problem is that each register used for variables will be one less that could be used for ordinary functions in memory. It''ll slow down the other variables slightly, which for games shouldn''t really be used unless there is one int that must be accessed over and over and over again.

Share this post


Link to post
Share on other sites
I wish I could understand what stoffel just said...::drools:: but for the half that I think I understand, I looks as if you are assigning a register pointer to an address. I thought that if a variable(or pointer) was in the register, that its memory location couldn''t be access/changed.

--Also still need an answer to my C question.

--After a few posts this board continues to rock.

--If anyone sees this please reply to my Win32 API problem. I really need to start being able to MAKE Windows Applications. You cant get far in a book without making something.

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Share this post


Link to post
Share on other sites
The register keyword is a hint to the compiler that you''ll be using the variable a lot so it should keep it where it can be accessed quickly. For typical Win32 on pc''s most compilers pretty much ignore it. My advice is to not worry about it in your code.

There may be specialized applications (such as compilers for embedded devices) that use it but it doesn''t sound like you''re concerned about that. (I''d think that in Stoffel''s case he should be using volatile not register but hey, special cases have special rules)

-Mike

Share this post


Link to post
Share on other sites