My question is simply, how many registers will a C compiler use for a variable declared using register
storage class as :
register int a
. I have read this answer How many register and what kind of register are available for the storage class REGISTER in c language but could not understand much. I know it can be implementation dependent, but then this registers are limited, so when would the compiler ignore the declaration or will it generate an error?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
C11 still supports
register
:In notes:
So how many registers will be used? One possible answer is 'none' because the keyword may be ignored.
That said, in practice it's very rare for an implementation on any modern platform to define
int
with a width that is wider than width of a typical platform 'word'. So if an implementation respectsregister int a;
it's unlikelyregister int a
will be allocated to more than one register andregister int a
may be 0,1 or 2 registers.Of course on some hardware not all registers are the same size and sometimes different instructions treat registers as separate or joined. How many registers may not be meaningful.
As others point out use of the keyword is widely discouraged in C. Indeed its meaning now removed from C++ (though the keyword retained as reserved).
I think that would be draconian in C which still looks to micro-controller environments where programmers come closest to want to dictate such details.
All that said 'back in the day' (25 years ago) I worked with a compiler that ignored
register
but allocated the first 2 (I think) declared variables used as loop counters to registers and in the case in question the third loop in the function was longest and performance noticeably improved by declaring the big loop index first.That all seems a very long time ago and no one should take it as the modern experience.
The
register
is a suggestion for the compiler to keep the variable in the register if possible. But the modern compilers just ignore it. So the answering your question - it can be zero or more the compiler will decide depending on the optimisation options used.Some compilers have special form of binging variables to the register.
gcc version
it can be global or local binding.
global binding: the register variable is kept in the register for the whole program execution, the register is taken out form the compiler register pool. It can make compiler optimisations less effective.
local binding: the register variable is tied to the register only in the scope of the function.
But it is not guaranteed that the library routines which were compiled without
-ffixed-reg
option will keep this register unaffected. Same is with thesetjump
&longjump
as restoring of the content of registers is machine specific.Deep understanding of the machine code generation is required to use it efficiently. Rarely used nowadays. Use examples - reducing latency in response to the interrupt requests and similar.