Reputation: 2933
As we know the register variable
is faster accessible
and it is upon the compiler
to create any variable of register variable
.
register int val;
I just want to know "Is there any situation/application where we have to only use register variable
and other variable is not useful on such cases?"
If you can put a piece of code it is good to understand.
Upvotes: 1
Views: 402
Reputation: 882316
"As we know" - actually we don't know that.
register
is a suggestion to the compiler, not a requirement. It's rarely needed with the insanely optimising compilers of today. From the current (C11) standard:
A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined.
So it could go into a register. It could also go into faster memory, regular memory or even out on disk, or on a server in Inner Mongolia, to punish coders for using unnecessary keywords :-)
You may find it useful in compilers that aren't quite so smart (perhaps some older, embedded ones, for example) but even, then, you don't have to use it - generally, you should forget about it. It's about as useful as auto
.
By way of example, let's assume a rather "less-than-optimising" compiler that treats every C instruction as a single unit. By that, I mean something like:
int x = 1;
x = x + 7;
x = x - 2;
useX (x);
will be converted into the assembly (a classic load-store architecture):
loadi r0, 1 ; set r0 to 1
stor r0, [x] ; store to memory
load r0, [x] ; load from memory
addi r0, 7 ; add 7
stor r0, [x] ; store to memory
load r0, [x] ; load from memory
subi r0, 2 ; subtract 2
stor r0, [x] ; store to memory
load r0, [x] ; load from memory
call useX ; call the function
In other words, no optimisation realising that the value is still in r0
following the addition so there's no need to load it up again.
In such a (brain-dead) compiler, the use of register
may give efficiencies that would be quite handy. The code:
register int x = 1;
x = x + 7;
x = x - 2;
useX (x);
may result in better code along the lines of:
loadi r0, 1 ; set r0 to 1
addi r0, 7 ; add 7
subi r0, 2 ; subtract 2
call useX ; call the function
All that stuff, of course, hinges on the compiler and how it uses registers. The code I've given is just a contrived example. As I stated, with modern compilers, it's rarely necessary.
Bottom line, in answer to your specific question "is there any situation/application where we have to only use register variable and other variable is not useful on such cases?", the answer is no. If it's truly a C compiler, there is no requirement to use the register
keyword.
Upvotes: 7
Reputation: 9725
There is a case when it worth not just hinting your compiler to allocate a register for a variable, but name a specific register for it (and then it is not allowed to ignore the register
keyword): implementing efficient threaded virtual machines.
A nice example of such approach is the OCaml bytecode interpreter.
And in any other case where you have to mix C and assembly code, manually mapping C variables to the registers is essential.
Upvotes: 1
Reputation: 78963
There is no situation where you have to use register
. But other than it seems to be urban legend, register
has a semantic meaning in a C program: variables that are declared with register
instead of auto
, the default, can't be subject to &
, so their address can't be taken. This means that they can never alias with other variables, and thus register
is merely an optimization hint, just as restrict
.
Upvotes: 3