Register variables

Hi
Is there any limit on the number of register variables can be used at any one time?
Does this limit depends on the number registers that the CPU holds?

In a large program is it wise to declare all the loop counter as register variables?

Thanx
Andonis Matsakas

The register keyword is just a hint to the compiler. There is no limit on how many times you can use that hint. There is also no guarantee that any of the variables will wind up with a somewhat dedicated register.

The register keyword is largely obsolete and I would not recommend using it for new code. Today's optimizers are very good at allocating registers where they are do the most good. And, by the way, today's risc and post-risc cpu's tend to have lots of registers to allocate. Registers aren't the scarce resource that they were in the 70's.

Hi,

I never clearly understood when to use the keyword "register", I just know that it is a request to the system to provide space for this object in the CPU registers, which are faster in accessing than RAM.

Can anyone provide me a small practical situation where it becomes imperative to use the keyword "register"?

I would also appreciate if someone suggests me a good book which emphasizes the use of registers.

I have searched the forums and found this thread

but it didn't answer my question - in which situations do we use them? I have seen industry strength C programs written by experts, which chose certain variables to be registers while declaring others as normal types, but couldn't quite appreciate why certain variable need to be a register.

Thank you!
Vishnu.

No one can give you an example where it is imperative to use the register keyword. It is never imperative. It never was. It never will be.

An example of where is might be useful is something like:

register int i;
register int tot;
int table[1000];
tot=0;
for(i=0; i<1000; i++)
    tot += table;

The idea is that if i stays in a register we can save some time. We don't want to store i back in memory after it gets incremented in the "for" statement and then read it back to use in the next statement. Same deal with tot. We don't want to store it in memory each iteration just to read it back so that we can add to it again.

But to make the above code actually useful, you must compile it with a very old compiler. Modern compilers will figure this out by themselves.

And you really need to be an assembler programmer to fully grasp this. You need to understand what a register is. And that machine language instructions move data between registers and memory. So the books to read are those on assembly language programming. Mastering assembler should clear this right up.