Register variables

R

root

Hi

I have a function called many times. It has a loop and I declare the
index variable as a "register int" to let the compiler know that it is
heavily used and should be optimized.

Now I need to modify the function and include a call in some loop
iterations to another function that will take a pointer to the loop index.

My question is: which will be more efficient?

(1) keep the register specifier and copy the variable to a non-register
variable when needed:
register int x;
int xcopy;
// ... (work with x)
xcopy = x;
function(&xcopy);
// ... (work with x)

or

(2) just use a single non-register variable:
int x;
// ...(work with x)
function(&x);

Thanks.


PS. In option (1) is it an undefined behavior to do
function(&(xcopy = x));
?
 
A

Andrey Tarasevich

root said:
I have a function called many times. It has a loop and I declare the
index variable as a "register int" to let the compiler know that it is
heavily used and should be optimized.

Now I need to modify the function and include a call in some loop
iterations to another function that will take a pointer to the loop index.

My question is: which will be more efficient?

(1) keep the register specifier and copy the variable to a non-register
variable when needed:
register int x;
int xcopy;
// ... (work with x)
xcopy = x;
function(&xcopy);
// ... (work with x)

or

(2) just use a single non-register variable:
int x;
// ...(work with x)
function(&x);

The latter. 'register' is one of those keywords that is pretty useless
with modern compilers. As far as I remember, it is deprecated in the
most recent versions of C standard (or am I thinking about C++
standard?). Just forget about 'register' and never look back.
PS. In option (1) is it an undefined behavior to do
function(&(xcopy = x));

No :) This is simply ill-formed. The result of an assignment operation
in C is not an L-value. You can't apply the unary '&' to it.
 
P

Peter Nilsson

Andrey Tarasevich said:
... 'register' is one of those keywords that is pretty useless
with modern compilers.

It's pretty useless when targetting cpus that are register
poor (e.g. intel) or register rich (PowerPC). For mid range
processors (e.g. 68000) it still comes in handy, although
I've found that using volatile to keep a variable _out_
of a register is more useful.
As far as I remember, it is deprecated in the
most recent versions of C standard...

Doesn't seem to be obsoleted in n1336.
 
M

Mark Bluemel

Hi

I have a function called many times. It has a loop and I declare the
index variable as a "register int" to let the compiler know that it is
heavily used and should be optimized.

Now I need to modify the function and include a call in some loop
iterations to another function that will take a pointer to the loop index.

The only reason you'd do this, AFAICS, is so that the second function
can change the loop index. At this point, I think you should look hard
at whether the use of a loop index is appropriate, or whether it would
be better for the loop to be controlled by the return value of the
second function, for example. As Eric has pointed out, this is a
recipe for incomprehensible code.
 
N

Nobody

I have a function called many times. It has a loop and I declare the index
variable as a "register int" to let the compiler know that it is heavily
used and should be optimized.

There's no point in using "register"; the compiler can almost certainly do
a better job of deciding what should go in a register than the programmer
can.
Now I need to modify the function and include a call in some loop
iterations to another function that will take a pointer to the loop index.

My question is: which will be more efficient?

(1) keep the register specifier and copy the variable to a non-register
variable when needed:
or

(2) just use a single non-register variable: int x;
// ...(work with x)
function(&x);

#2.

Not only will the compiler figure out when to store the variable in a
register, it will figure out whether to store the variable at all. E.g. if
you have:

extern int a[];
int i;
for (i = 0; a >= 0; i++)
foo(a);

it may decide that i itself is unnecessary, and use:

extern int a[];
int *p;
for (p = a; *p >= 0; p++)
foo(*p);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top