468,507 Members | 1,583 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,507 developers. It's quick & easy.

Question on Register variables

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.

Oct 9 '06 #1
7 4810

int main(void) wrote:
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

Oct 9 '06 #2

int main(void) wrote:
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.

Oct 9 '06 #3
In article <11**********************@i42g2000cwa.googlegroups .com>,
int main(void) <dn****@gmail.comwrote:
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
Oct 9 '06 #4
Nishu wrote:
By default variables have auto storage class
Only if the variable is defined outside of a function definition.

--
pete
Oct 9 '06 #5
pete <pf*****@mindspring.comwrote:
Nishu wrote:
By default variables have auto storage class

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
Oct 9 '06 #6
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.

Oct 10 '06 #7
Richard Bos wrote:
>
pete <pf*****@mindspring.comwrote:
Nishu wrote:
By default variables have auto storage class
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).
Yes.
I wrote something different from what I was thinking.

--
pete
Oct 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bryan Parkoff | last post: by
10 posts views Thread by ajay | last post: by
15 posts views Thread by sethukr | last post: by
28 posts views Thread by sowmiyakc18 | last post: by
26 posts views Thread by Vashna | last post: by
1 post views Thread by Sudhakar | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.