register variables

J

junky_fellow

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

Arafangion

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.
 
P

pete

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.
 
J

Jonathan Bartlett

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
 
M

Meghavvarnam

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
 
F

Flash Gordon

Arafangion said:
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.
 
R

Roger Leigh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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.sourceforge.net/>

iD8DBQFCiN82VcFcaSW/uEgRAnm/AJ4oIJ/V1JtlU6yr7u8eOH9VOvNX6gCguZ8J
LO4Dx52ccJ1QmKW6gzTXSf0=
=yO8R
-----END PGP SIGNATURE-----
 
M

Mark McIntyre

On 16 May 2005 05:07:35 -0700, in comp.lang.c ,
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...
 
K

Keith Thompson

Meghavvarnam said:
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.
 
B

Ben Pfaff

Jonathan Bartlett said:
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.
 
J

James McIninch

<posted & mailed>

what is the purpose of declaring a register variable ?

It's a hint to the compiler that you think this variable is going to be
accessed frequently, so you'd like it stored in a CPU register rather than
in memory.

why can't we find the address of register variable ?

CPU registers don't have addresses.
 
K

Keith Thompson

Ben Pfaff said:
This is usually but not always true. Some CPUs put the registers
into addressable memory.

Right. You can't take the address of a register variable because the
standard says you can't take the address of a register variable. The
original rationale for that is based on CPUs for which registers don't
have addresses, but the language could have allowed it. A compiler is
free to use a register for a variable not declared with the "register"
keyword; it's also free not to use a register for a variable declared
with the "register" keyword. It's nothing more than a hint to the
compiler.
 
J

Jack Klein

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.

Your knowledge of processor architectures seems quite limited. There
are quite a few processors in current production where registers also
have ordinary memory addresses and can indeed be accessed like RAM.
 
J

Jack Klein

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



There is no purpose nowadays. The compiler can do a much better
analysis of which variables would benefit from being stored in a
register.

People often make the statement these days that no programmer can
possibly outdo the compiler at register use, with a the unstated
assumption that there are no possible exceptions to this rule.

I have yet to see anyone making this claim offer any proof that it is
true.

I have also yet to see an implementation that I could not beat, at
least on specific functions where I know more about the likely values
of various variables than the compiler can deduce from data flow
analysis.
 
J

Jack Klein

<posted & mailed>



It's a hint to the compiler that you think this variable is going to be
accessed frequently, so you'd like it stored in a CPU register rather than
in memory.



CPU registers don't have addresses.

....on the processors you are familiar with. There are quite a few
hardware architectures where they do have addresses, although that is
irrelevant to C.

ISTR the TI9900 series that kept all its registers in RAM, allocating
new blocks for new context frames. Worked relatively well back in the
days when processors were slow and memory speed was not a bottleneck.
 
J

Jaspreet

what is the purpose of declaring a register variable ?

I thought it was just a request to the compiler to let it know the
programmer wants that variable to be stored on a register. It is in no
way a command to the compiler which it has to follow. It depends on the
compiler optimisation.

Nowadays, it hardly depends because the compilers are lot more
intelligent and can decide for themselves how to optimise the code.

I also think that we can only have integer variable stored in a
register. I think float or double cannot be stored on a register.

Please let me know if I am wrong.
 
B

Ben Pfaff

Jaspreet said:
I also think that we can only have integer variable stored in a
register. I think float or double cannot be stored on a register.

You can request any kind of variable be stored in a register.
The compiler may ignore the request.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top