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

Home Posts Topics Members FAQ

register variables

what is the purpose of declaring a register variable ?
why can't we find the address of register variable ?

Nov 14 '05 #1
16 2456
ju**********@ya hoo.co.in wrote:
what is the purpose of declaring a register variable ?
why can't we find the address of register variable ?


Register variables are *insanely* fast, in a nutshell.
I would assume that you can't find the address of a register variable on
the basis that they do not *have* an address, it simply is 'register
number 2', from what little.

Normal RAM is many, many times slower, in comparison.
Nov 14 '05 #2
ju**********@ya hoo.co.in wrote:

what is the purpose of declaring a register variable ?
It tells the compiler that you think you have a better
idea about how to optimise code.
why can't we find the address of register variable ?


That's the one guaranteed effect of using the register qualifier.

--
pete
Nov 14 '05 #3
To add to Pete here is what I have to say :

1. Declaring a register variable, suggests the compiler to use a
register on the processor exclusively for that variable and hence
access to this variable becomes incredibly fast.

2. Any register on the processor is identified by its respective name
unlike memory locations on RAM that have addresses. To know the
registers on a given processor, please look at the processor manuals. I
dont know if they are available online. Am sure they should be.

Thank you,
Megh

Nov 14 '05 #4
Arafangion wrote:
ju**********@ya hoo.co.in wrote:
what is the purpose of declaring a register variable ?
why can't we find the address of register variable ?
Register variables are *insanely* fast, in a nutshell.


Not always.
I would assume that you can't find the address of a register variable on
the basis that they do not *have* an address, it simply is 'register
number 2', from what little.
Yes and no. In C you can't take the address of a register variable
because the language definition says you can't. The reason the language
definition says that is because on some (not all) processors registers
do not have addresses.
Normal RAM is many, many times slower, in comparison.


Not always.

The compiler does not have to put registers in variables when they are
defined as register variable. The compiler is allowed to put variable in
registers that are *not* defined as register variable, and generally
compilers are good at doing this where it makes sense. A lot of modern
processors have data cache, so even if the variable is in memory, it
might only exist in the data cache which is fast. There are also systems
(for which I've written SW in C) where the memory only takes a single
cycle to access, so it is not always slower than registers.

The register keyword is a hint to the compiler which has the side effect
of preventing you from taking the address of a variable. It does not
guarantee anything in terms of performance and, with modern compilers, I
do not bother using it.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #5
ju**********@ya hoo.co.in wrote:
what is the purpose of declaring a register variable ?
A register is a kind of "working scratch space" for the CPU. Registers
are on the chips themselves, so there is no delay accessing the
registers like there is for memory. Computers usually only have a few
registers, therefore, the compiler has to determine which variables need
to be in registers at what times for the best speed. This is not a
perfect calculation. Declaring a variable as "register" says that you
want to force the compiler to keep a given variable in a register, even
if its own optimization procedure would not do so.
why can't we find the address of register variable ?


Registers are part of the CPU, not memory. Therefore, registers do not
have a memory address.

If you want to know more about how the CPU works at the core instruction
level, you should check out my book, Programming from the Ground Up:

http://www.cafeshops.com/bartlettpublish.8640017

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ju**********@ya hoo.co.in writes:
what is the purpose of declaring a register variable ?
There is no purpose nowadays. The compiler can do a much better
analysis of which variables would benefit from being stored in a
register.
why can't we find the address of register variable ?


Because addresses are memory locations, while registers are part of
the CPU. They don't /have/ an address.
- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourc eforge.net/>

iD8DBQFCiN82VcF caSW/uEgRAnm/AJ4oIJ/V1JtlU6yr7u8eOH 9VOvNX6gCguZ8J
LO4Dx52ccJ1QmKW 6gzTXSf0=
=yO8R
-----END PGP SIGNATURE-----
Nov 14 '05 #7
On 16 May 2005 05:07:35 -0700, in comp.lang.c ,
ju**********@ya hoo.co.in wrote:
what is the purpose of declaring a register variable ?
why can't we find the address of register variable ?


I have a thought: why not publish your entire paper in one go instead
of one question at a time, and then maybe someone can work out which
class you're attending, and mail the answers direct to your teacher...
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
Nov 14 '05 #8
"Meghavvarn am" <me********@yah oo.com> writes:
To add to Pete here is what I have to say :

1. Declaring a register variable, suggests the compiler to use a
register on the processor exclusively for that variable and hence
access to this variable becomes incredibly fast.


The compiler, especially if invoked in an optimizing mode, will
attempt to analyze your program to determine which variables should be
assigned to registers for the best possible performance. It can even
assign a variable to a register for part of its lifetime, and use the
same register for a different variable in a different part of the
code, something you can't easily do with the "register" keyword.
example:

Registers typically don't have addresses, but you can still legally
take the address of x or y. If you do so in a section of code where
the compiler has decided to assign the variable to a register, the
compiler will simply not assign it to a register for that section of
code; it's the compiler's job to generate code that behaves properly.

According to the common wisdom these days, the main effect of using
the "register" keyword is to interfere with the compiler's
determination of which variables should be assigned to registers.
Modern compilers are probably better at making this determination than
you are (or than I am). (This wasn't the case for the compilers that
existed when the register keyword was introduced. Times change.)

Bottom line: Don't bother using the "register" keyword. Leave the
compiler alone and it will do a better job of optimization than you
can.

--
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.
Nov 14 '05 #9
Jonathan Bartlett <jo*****@eskimo .com> writes:
ju**********@ya hoo.co.in wrote:
why can't we find the address of register variable ?


Registers are part of the CPU, not memory. Therefore, registers do
not have a memory address.


This is usually but not always true. Some CPUs put the registers
into addressable memory.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Nov 14 '05 #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:
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
14
7904
by: aruna | last post by:
What is the disadvantage of using register storage class specifier?
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
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
3261
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
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
7
1847
by: RANNA | last post by:
Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if want to declare 10 register variables in my code, is it possible to do it?
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...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
0
7199
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
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
5575
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();...
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.