Willem said:
Ioannis wrote:
) Richard wrote:
)>> If the function is more than 5-6 lines of code, "register" should
not be )>> used for the function scope but only for local scopes in
the function )>> (like small loops).
)>
)> Where do you get these "facts" that you maintain are true with zero
to )> back them up? It all depends on the compiler and also depend, in
a )> larger function, on how often your register variable was used and
how it )> was used. Length in lines has absolutely nothing to do with
it. )
) The smaller the scope, the less effect it has on the rest of code.
Where ) do you disagree with this?
You're arguing in circles again.
Why should 'register' be used only when it has minimal effect on the
code ?
He probably means minimal adverse effect (WRT efficiency) on the rest of
the code.
The only true way to determine the effectiveness of register and it's
effects on code surrounding it is to compile it for a particular
implementation and examine the assembler so generated. To determine
with even more assurance you'll have to see the source for the
compiler, or at least the relevant portions.
It's possible that register will influence the code generated and how
exactly it does so depends on the exact code, compiler flags, and the
register allocation algorithms used by the compiler.
A trivial example could be that declaring an object with register forced
the compiler to load and store from memory some other object that would
otherwise have benefited from being in a register.
Consider a case of a nested loop:
for (register int i = 0; i < UPPER_BOUND; i++) {
for (int j = INIT_VALUE; j > 0; j--) {
/* do something */
}
}
Now any decent compiler will try to place 'j' in a register in
preference over 'i', but it may be that it decided to heed the
programmer's suggestion and used a register for 'i', with a probable
loss in performance. I seriously doubt that any compiler is this bad. I
think that if they determine that an explicit use of register is
causing more loads and stores than otherwise, they just ignore the
suggestion.