473,725 Members | 2,368 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 5159

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
1694
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
15658
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
3495
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
3623
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
3752
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
1851
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
3567
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
2224
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
1392
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
8882
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8749
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
9398
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
9098
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...
0
8080
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6699
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
6007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4777
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.