473,626 Members | 3,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5142

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************ **********@i42g 2000cwa.googleg roups.com>,
int main(void) <dn****@gmail.c omwrote:
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*****@mindsp ring.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*****@mindsp ring.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
1691
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 before they are stored into memory location (variable storage). .....Please take a look at two functions below. Byte variable and Carry variable are global variables outside of functions. .....Do you notice that there are two duplicated lines...
10
15651
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 size) ? tx Ajay
20
3473
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 "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
4
3616
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
16
3741
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 than the processor has, how is it handled by the C compiler? regards
15
1842
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
3560
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 functions?
26
2212
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 a register variable and this will make it much faster to access. Now the question is: obviously there are only a fixed number of registers in our CPU, maybe 6 or something. So how do we choose which
1
1387
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 website.com/thankyou.php if they enter the url directly in the browser as website.com/thankyou.php they can access the file, if a user accesses the file this way i would like to redirect to a page saying "Direct acess to this file is not allowed"
0
8202
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8510
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.