Question on Register variables

I

int main(void)

Hi all,

I know that register variables work fine at block scope.
I tried putting a register variable in file scope, like this,

#include <stdio.h>
register int a = 2;
int main(void)
{
printf("%d\n",a);
}

But my compiler gives me an error,
error: register name not specified for 'a'
Now, i have 2 questions

1) Why is it not possible to have register variables at file scope ?

2) Is it necessary that a variable should be made register variable
(or) automatically compiler will make it register variable if it is
neccessary.


Regards,
Yugi.
 
N

Nishu

int said:
Hi all,

I know that register variables work fine at block scope.
I tried putting a register variable in file scope, like this,

#include <stdio.h>
register int a = 2;
int main(void)
{
printf("%d\n",a);
}

But my compiler gives me an error,
error: register name not specified for 'a'
Now, i have 2 questions

1) Why is it not possible to have register variables at file scope ?

2) Is it necessary that a variable should be made register variable
(or) automatically compiler will make it register variable if it is
neccessary.

By default variables have auto storage class (goes onto the stack).
Compiler may specify some most commonly used variables with register
storage class. This is one step in optimization.

And now you may appreciate the deprecation of register variable for the
global scope. It simply consumes your one register thro' out the
execution, which is certainly not what you wanted.

-Nishu
 
R

robertwessel2

int said:
Hi all,

I know that register variables work fine at block scope.
I tried putting a register variable in file scope, like this,

#include <stdio.h>
register int a = 2;
int main(void)
{
printf("%d\n",a);
}

But my compiler gives me an error,
error: register name not specified for 'a'
Now, i have 2 questions

1) Why is it not possible to have register variables at file scope ?


"register" is specifically prohibited for external declarations. If it
were allowed, the poor compiler and linker are going to have to figure
out how to do the register allocation across multiple translation
units.

2) Is it necessary that a variable should be made register variable
(or) automatically compiler will make it register variable if it is
neccessary.


Most compilers do a better job figuring out what variables belong in
registers than do most programmers, and many compilers ignore the
register keyword when determining which variables to assign to which
registers.
 
R

Richard Tobin

int main(void) said:
I tried putting a register variable in file scope, like this,

That's not legal C.
But my compiler gives me an error,
error: register name not specified for 'a'

This suggests that your compiler allows it as an extension, but
requires you (somehow) to specify the register to use. But that's
just a guess.
1) Why is it not possible to have register variables at file scope ?

It's not impossible in principle, it's just something that C doesn't
have. And that's probably because it's awkward to implement with
separate compilation of files.
2) Is it necessary that a variable should be made register variable
(or) automatically compiler will make it register variable if it is
neccessary.

Compilers can and do put variables in registers without you needing to
declare it. Modern compilers can probably choose which variables to
put in registers better than you can.

-- Richard
 
R

Richard Bos

pete said:
Only if the variable is defined outside of a function definition.

_Inside_ of a function definition. Outside, they're static (and not just
by default, but always).

Richard
 
S

Samuel Stearley

Despite the sentiment that the compiler is always smarter than you
there are cases where the register keyword can be useful.
A couple months back I found an interesting case.
In suedo code it looks like this


LOOP:
1: Tell hardware to put 4KB of data from its ram to a buffer available
in PCI space.
2: Tell bridge chip to DMA that 4KB from PCI to RAM
3: While DMA is ongoing process a previously finished DMA.
4: Wait for ongoing DMA to finish. (But it might have already
finished during 3)
5: Toggle buffers so on next iteration step 3 processes the newly
finished DMA.
REPEAT


I found that by declaring all variables associeated of step 3 with the
register keyword the performance improved significantly. The
bottleneck is now entirely the 33 MHz PCI bus, E.G when it gets to step
4 it no longer finds that DMA finished during step 3.

The platform is an 800 MHz power pc G4, The compiler is gcc 3.4.
If the target hardware is x86 I wouldn't ever bother with the register
keyword. But power pc's have 32 general purpose registers.
 
P

pete

Richard said:
_Inside_ of a function definition.
Outside, they're static (and not just by default, but always).

Yes.
I wrote something different from what I was thinking.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top