473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 5123

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bryan Parkoff | last post by:
.....I try to reduce un-necessary temporal variables so it can be optimized for best performance. I try to assign only one register storage so two variables can access to only one register storage...
10
by: ajay | last post by:
some say floats can't be stored in register. size of int and float are same ( on some machine am i Wrong?) so if int can be delcared as register than why not float(where in and float are of same...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
16
by: alakesh.haloi | last post by:
Hi All Is there a limit on the number of register variables that can be declared and initialized in a C program on a particular hardware platform? If you define more number of register variables...
15
by: sethukr | last post by:
Hi everybody, While running the following program in GCC, i'm very much screwed. main() { char *ptr1; char arr; int i; char *ptr2;
28
by: sowmiyakc18 | last post by:
Please clear my doubt. When do we declare a variable to be a register variable? What is its significance? What are the conditions to be adhered to when register variables are passed between...
26
by: Vashna | last post by:
Hi Group, I have a doubt about register variables. I know that if we have a variable used very frequently in a function, then provided we never apply the & function to it, we can define it as...
1
by: Sudhakar | last post by:
i have a registration page called register.php if the data entered is validated correctly i call a file called thankyou.php or else validate.php presently a user after seeing the url...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.