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

Home Posts Topics Members FAQ

Register variables doubt

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
variables to define as register variables? Obviously things like loop
counters or frequently accessed pointers we must define as register, but
what other criteria do people use?

Thanks.

Oct 5 '07 #1
26 2211
Vashna wrote:
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
variables to define as register variables? Obviously things like loop
counters or frequently accessed pointers we must define as register, but
what other criteria do people use?
http://www.c-faq.com is the Frequently Asked Questions database for this
newsgroup.

You probably need to start with Question 20.13
Oct 5 '07 #2
Vashna wrote:
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.
Furthermore external objects are not permitted to be qualified with
register.

Also stating with certainty that an object qualified with register will be
accessed "much faster" is not correct. The compiler is free to ignore the
qualifier.
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
variables to define as register variables? Obviously things like loop
counters or frequently accessed pointers we must define as register, but
what other criteria do people use?
With most modern compilers programmers do not use the register qualifier at
all. The reason is that the compiler's optimiser is likely to almost always
to better register allocation than the programmer.

Oct 5 '07 #3
>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.
You should not know that it will make it faster, that it will make
it much faster, nor that it will make it slower or much slower.
But it might. The compiler is free to ignore the register declaration
entirely.
>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
variables to define as register variables?
I generally let the compiler do its job and define none. Dedicating
a register might slow down other calculations that need it more.
>Obviously things like loop
counters or frequently accessed pointers we must define as register, but
what other criteria do people use?
If you have MEASURED the performance both ways, and register improves
performance, go ahead and use it, but be prepared to MEASURE again when
moving to a different platform / compiler / OS.

Oct 5 '07 #4
Vashna wrote:
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
variables to define as register variables? Obviously things like loop
counters or frequently accessed pointers we must define as register, but
what other criteria do people use?
As others have said, register is freely ignored by the compiler, and the
compiler almost always does a better job of register allocation than the
programmer. If a variable would be better stored in a register, the
compiler will work that out for you and put it there.

Just don't use the 'register' keyword. Like 'auto', there is no need for
it. (Unlike 'auto', it is conceivable that in some old compilers there
was a need for 'register'.)

--
Philip Potter pgp <atdoc.ic.ac. uk
Oct 5 '07 #5
Vashna wrote:
>
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
variables to define as register variables?
We can only choose which variables to define as register variables
for specific C implementations .
Obviously things like loop
counters or frequently accessed pointers we must define as register,
No.
The register keyword is *not* something that can be used
to good effect for a program which is intended
to run on arbitrary C implementations .
but what other criteria do people use?
The register keyword tells the compiler
that the code writer thinks that he knows
more about optimization than the compiler does.

Use the register keyword when you know more
about optimization than your compiler does;
and get ready to remove the register keyword from your code
if the code is supposed to be able to run on other C implementations .

--
pete
Oct 5 '07 #6
pete wrote:
Vashna wrote:
>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 variables to define as register variables?

We can only choose which variables to define as register
variables for specific C implementations .
>Obviously things like loop counters or frequently accessed
pointers we must define as register,

No. The register keyword is *not* something that can be used
to good effect for a program which is intended to run on
arbitrary C implementations .
>but what other criteria do people use?

The register keyword tells the compiler
that the code writer thinks that he knows
more about optimization than the compiler does.

Use the register keyword when you know more
about optimization than your compiler does;
and get ready to remove the register keyword from your code
if the code is supposed to be able to run on other C implementations .
You have it all wrong. The register keyword may always be ignored
by the compiler, with the sole exception that it makes taking the
address of the object impossible. This allows the compiler to
completely control the optimization. So using it (register) is
pointless, except on some ancient compilers that can't do
optimization at all.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 6 '07 #7
CBFalconer <cb********@yah oo.comwrites:
pete wrote:
[...]
>The register keyword tells the compiler
that the code writer thinks that he knows
more about optimization than the compiler does.

Use the register keyword when you know more
about optimization than your compiler does;
and get ready to remove the register keyword from your code
if the code is supposed to be able to run on other C implementations .

You have it all wrong. The register keyword may always be ignored
by the compiler, with the sole exception that it makes taking the
address of the object impossible. This allows the compiler to
completely control the optimization. So using it (register) is
pointless, except on some ancient compilers that can't do
optimization at all.
That's the common widom, but I wonder how accurate it is.

First, here's what the standard says (C99 6.7.1p4):

A declaration of an identifier for an object with storage-class
specifier register suggests that access to the object be as fast
as possible. The extent to which such suggestions are effective is
implementation-defined.

with a footnote:

The implementation may treat any register declaration simply as an
auto declaration. However, whether or not addressable storage is
actually used, the address of any part of an object declared with
storage-class specifier register cannot be computed, either
explicitly (by use of the unary & operator as discussed in
6.5.3.2) or implicitly (by converting an array name to a pointer
as discussed in 6.3.2.1). Thus, the only operator that can be
applied to an array declared with storage-class specifier register
is sizeof.

Do modern compilers really ignore the "register" keyword (except to
prohibit taking the address of the object)? Or do some of them pay
some attention to it, at least in some modes?

It seems at least plausible that, if I've carefully profiled my code
and found that references to a particular int object are a bottleneck,
declaring it "register" at least is unlikely to hurt.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 6 '07 #8
Thanks for all the answers, I feel like I understand it much better now.

The only doubt I still have is: what if in a single function we define
more register variables than there are registers in the CPU? Will we get
a compile time error, or just unpredictable behavior at runtime?

(Actually I guess you may not even be able to have one register variable
for each register, if the return value of the function is passed back to
the caller in a register.)

Thanks.

Oct 6 '07 #9
Vashna wrote:
Thanks for all the answers, I feel like I understand it much better now.

The only doubt I still have is: what if in a single function we define
more register variables than there are registers in the CPU? Will we get
a compile time error, or just unpredictable behavior at runtime?
Neither. The implementation will simply ignore one or more or all of your
register requests.

<snip>

Oct 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2250
by: Alex | last post by:
I apoligise in advance if this is an os or platform based question, I don't know. I was wondering how register integers (and other types of register variables) are managed by c++. For example, on a pentium 4, there are 8 register integers in the cpu. If you define more than 8, or if there are other programs using this space, how are the variables allocated. This is for a simulation program that needs to be very fast. For example:
6
4905
by: jacob navia | last post by:
As far as I understood the standard, non-automatic variables (static/global data) can't be cached in registers because in a multiprocessing or multi-threading environment, another thread/processor could read a wrong value from main memory, if the compiler would do so Even if the variable has local scope (static variable enclosed in a function scope) this would still hold since another processor/thread could run the same code at the same...
16
3740
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
7
5142
by: int main(void) | last post by:
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);
33
3260
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a full 10 minutes every time a "register" declared variable is read from or written to. Besides this lag, everything else runs as expected. Then my compiler is still C compliant, aye? If so, then it is unwise for any programmer to ever use the...
28
3558
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?
0
8259
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
8192
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
8696
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
8637
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
8502
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
5571
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
4090
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
2621
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
2
1504
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.