CBFalconer <cb********@yah oo.comwrites:
pete wrote:
[...]
>The register keyword tells the compiler
that the code writer thinks that he knows
more about optimization than the compiler does.
Use the register keyword when you know more
about optimization than your compiler does;
and get ready to remove the register keyword from your code
if the code is supposed to be able to run on other C implementations .
You have it all wrong. The register keyword may always be ignored
by the compiler, with the sole exception that it makes taking the
address of the object impossible. This allows the compiler to
completely control the optimization. So using it (register) is
pointless, except on some ancient compilers that can't do
optimization at all.
That's the common widom, but I wonder how accurate it is.
First, here's what the standard says (C99 6.7.1p4):
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.
with a footnote:
The implementation may treat any register declaration simply as an
auto declaration. However, whether or not addressable storage is
actually used, the address of any part of an object declared with
storage-class specifier register cannot be computed, either
explicitly (by use of the unary & operator as discussed in
6.5.3.2) or implicitly (by converting an array name to a pointer
as discussed in 6.3.2.1). Thus, the only operator that can be
applied to an array declared with storage-class specifier register
is sizeof.
Do modern compilers really ignore the "register" keyword (except to
prohibit taking the address of the object)? Or do some of them pay
some attention to it, at least in some modes?
It seems at least plausible that, if I've carefully profiled my code
and found that references to a particular int object are a bottleneck,
declaring it "register" at least is unlikely to hurt.
--
Keith Thompson (The_Other_Keit h)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"