473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Disadvantages of register storage class specifier

What is the disadvantage of using register
storage class specifier?
Nov 14 '05 #1
14 7885
aruna wrote:
What is the disadvantage of using register
storage class specifier?


Do your own homework, you will get better answers
in a book than you will get here.

--
Thomas.

Nov 14 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

aruna wrote:
What is the disadvantage of using register
storage class specifier?


In my opinion, the primary disadvantage is that you can't take the
address of a register variable.

Again, my opinion, but the 'register' storage class doesn't provide much
of an advantage when it is used: the compiler is permitted to ignore it
when selecting placement of variables, compiler optimization of
generated code may do a better job of optimizing placement than the
programmer can, and the language imposes restrictions on the use of
variables defined as register.

- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAj9fCagVFX4UWr64RApRVAKCfs0M3p2yECKZbWJd4Yp w0fj91vwCfRlsu
IT1RBBQIMrWWkTLjc1yVdTY=
=L0q5
-----END PGP SIGNATURE-----
Nov 14 '05 #3
Lew Pitcher <Le*********@td.com> writes:
aruna wrote:
What is the disadvantage of using register
storage class specifier?
In my opinion, the primary disadvantage is that you can't take the
address of a register variable.


One could argue that that's an advantage. The "register" specifier is
a way of asserting to the compiler that a given variable won't have
its address taken. Theoretically a compiler could use that
information for optimization, even if it doesn't use it for its
original intent (storing the variable in a CPU register).

If my goal were to give the compiler as much information as possible,
I might even use a "register" specifier on *every* variable whose
address isn't taken. But the result wouldn't be pretty, and it
probably wouldn't do much good; any decent modern optimizing compiler
should be able to figure this out for itself.
Again, my opinion, but the 'register' storage class doesn't provide much
of an advantage when it is used: the compiler is permitted to ignore it
when selecting placement of variables, compiler optimization of
generated code may do a better job of optimizing placement than the
programmer can, and the language imposes restrictions on the use of
variables defined as register.


That's certainly the common wisdom, and I suspect compiler
implementers take that into account when deciding how much attention
to pay to "register" specifiers (i.e., not much). I've even read that
"register" can hurt optimization by interfering with the compiler's
own analysis, but I don't know if that's actually true.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #4
Keith Thompson <ks***@mib.org> spoke thus:
One could argue that that's an advantage. The "register" specifier is
a way of asserting to the compiler that a given variable won't have
its address taken. Theoretically a compiler could use that
information for optimization, even if it doesn't use it for its
original intent (storing the variable in a CPU register).
Setting aside theory, do any real implementations optimize based on
the fact that a given variable's address is never taken, aside from
placing that variable in a register or not?
to pay to "register" specifiers (i.e., not much). I've even read that
"register" can hurt optimization by interfering with the compiler's
own analysis, but I don't know if that's actually true.


I imagine that "register" can only hurt optimization as much as the
compiler lets it. It's a hint that I imagine is nowadays routinely
ignored by compilers.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5
In article <a2**************************@posting.google.com >,
ar********@yahoo.co.in (aruna) wrote:
What is the disadvantage of using register
storage class specifier?


It requires typing seven characters, and it disallows usage of the
address operator. In some cases, it causes undefined behaviour.

Now ask for the advantages...
Nov 14 '05 #6
In article <c6**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Keith Thompson <ks***@mib.org> spoke thus:
One could argue that that's an advantage. The "register" specifier is
a way of asserting to the compiler that a given variable won't have
its address taken. Theoretically a compiler could use that
information for optimization, even if it doesn't use it for its
original intent (storing the variable in a CPU register).


Setting aside theory, do any real implementations optimize based on
the fact that a given variable's address is never taken, aside from
placing that variable in a register or not?


First, real implementations are more clever; even if the address of a
variable is taken, it is often possible to determine the set of accesses
to the variable exactly (this would be in practise more important for
C++ compilers + trivial classes + inline functions, but many compilers
implement both C and C++).

Second, if you know exactly all accesses to a variable, then you also
know that there is no aliasing, and that will help a lot. For example:

struct { int x; int y; int z; } t;
int *p;

t.x = 1; *p = 2;
t.y = t.x + 1; *p = 3;
t.z = t.x + t.y + 1; *p = 4;

Even if t.x, t.y and t.z are not in registers, the compiler may be able
to find that t.x, t.y and t.z are set to 1, 2 and 4, and *p needs only
be set once.
Nov 14 '05 #7
Christian Bau wrote:

In article <a2**************************@posting.google.com >,
ar********@yahoo.co.in (aruna) wrote:
What is the disadvantage of using register
storage class specifier?


It requires typing seven characters, [...]


Christian is a *fast* typist ...

--
Er*********@sun.com
Nov 14 '05 #8

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c6**********@chessie.cirr.com...
Keith Thompson <ks***@mib.org> spoke thus:
One could argue that that's an advantage. The "register" specifier is
a way of asserting to the compiler that a given variable won't have
its address taken. Theoretically a compiler could use that
information for optimization, even if it doesn't use it for its
original intent (storing the variable in a CPU register).


Setting aside theory, do any real implementations optimize based on
the fact that a given variable's address is never taken, aside from
placing that variable in a register or not?

Yes, if gcc is a real implementation. No, if you don't count registerization
as an optimization. I have bug issues pending against a commercial compiler,
where taking an address inhibits optimization more than it should.
Nov 14 '05 #9
Eric Sosman wrote:
Christian Bau wrote:

In article <a2**************************@posting.google.com >,
ar********@yahoo.co.in (aruna) wrote:
What is the disadvantage of using register
storage class specifier?


It requires typing seven characters, [...]


Christian is a *fast* typist ...


Indeed. Can't say much about readability though. Its like
programming while pissed ;)

rgistr cst unsigd sht x;
Nov 14 '05 #10
In <a2**************************@posting.google.com > ar********@yahoo.co.in (aruna) writes:
What is the disadvantage of using register storage class specifier?


The same as its advantage: the compiler might take it seriously.

If the compiler is good at performing optimisations on its own, this
is a disadvantage. If the compiler doesn't even try to optimise, this is
an advantage.

To a good optimising compiler, forcing a variable to be kept in a register
for the whole duration of a function's execution is usually a disadvantage
because it can decide better when keeping that variable in a register
makes sense. Fortunately, such compilers usually ignore this part of the
register keyword semantics and only retain the "no unary & operator" bit.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
Christian Bau wrote:
What is the disadvantage of using register
storage class specifier?

It requires typing seven characters, and it disallows usage of the
address operator. In some cases, it causes undefined behaviour.


I would say nine characters.

Nov 14 '05 #12
In <40***********************@news.xs4all.nl> Case <no@no.no> writes:
Christian Bau wrote:
What is the disadvantage of using register
storage class specifier?

It requires typing seven characters, and it disallows usage of the
address operator. In some cases, it causes undefined behaviour.


I would say nine characters.


#define regist register

and Christian is right ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13
Dan Pop wrote:

In <a2**************************@posting.google.com > ar********@yahoo.co.in (aruna) writes:
What is the disadvantage of using register storage class specifier?


The same as its advantage: the compiler might take it seriously.

If the compiler is good at performing optimisations on its own, this
is a disadvantage. If the compiler doesn't even try to optimise, this is
an advantage.

To a good optimising compiler, forcing a variable to be kept in a register
for the whole duration of a function's execution is usually a disadvantage
because it can decide better when keeping that variable in a register
makes sense. Fortunately, such compilers usually ignore this part of the
register keyword semantics and only retain the "no unary & operator" bit.


Further to Dan's point, I've seen code that was "pessimized"
by over-enthusiastic use of `register'. The platform in question
had only a few "scratch registers," and any additional registers
the compiler chose to use had to be saved and restored at function
entry and exit. When the excessive `register' declarations forced
virtually all the local variables into registers, the compiler had
to generate the extra code and memory references to make the extra
registers available. Not a good trade-off for a variable that was
only going to be referenced two or three times ...

In the Early Days when compilers had less memory and fewer
CPU cycles to spend on analyzing and optimizing the code, the
`register' keyword could be useful. Even then, though, it was
"non-portable" in the sense that you didn't really know what effect
it would have on different target platforms. On a register-rich
machine it might pay to use a lot of `register' variables, but the
same tactic could (as recounted above) just as well be harmful.
I once ran across a body of code that tried to accommodate this
difference with definitions like

#ifdef MACHINE_A /* a register-rich machine */
#define REGISTER1 register
#define REGISTER2 register
#define REGISTER3 register
#define REGISTER4 register
#endif
#ifdef MACHINE_B /* a register-poor machine */
#define REGISTER1 register
#define REGISTER2 /* nil */
#define REGISTER3 /* nil */
#define REGISTER4 /* nil */
#endif

.... and then you'd declare the "most important" variable in a
function as `REGISTER1', the "runner-up" as `REGISTER2', and so
on. I don't know how effective the approach actually turned out
to be (I didn't spend much time with the code in question), but
I'm glad such shenanigans have become mostly superfluous.

--
Er*********@sun.com
Nov 14 '05 #14
Dan Pop wrote:
In <40***********************@news.xs4all.nl> Case <no@no.no> writes:
Christian Bau wrote:
What is the disadvantage of using register
storage class specifier?
It requires typing seven characters, and it disallows usage of the
address operator. In some cases, it causes undefined behaviour.


I would say nine characters.

#define regist register

and Christian is right ;-)

Dan


But that's 23 (not counting the newline). It should be

#define seven_characters register

:-)

Kees/Case

Nov 14 '05 #15

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

Similar topics

1
by: ravi_shankar | last post by:
I know java,but I am just beginner in C.I have some confusions regarding extern storage specifier and default storage class specifier for a variable when it has file scope that is ,when it is not...
10
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...
16
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...
29
by: orium69 | last post by:
hi everyone, i'm wondering if there is a way to have sure that a variable is allocated in the cache, after its declaration with "register"? Tks!
33
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...
1
by: sunny | last post by:
Hi C Ref Man says " The typedef specifier does not reserve storage and is called a storage class specifier only for syntactic convenience " does this mean storage specifier for "user...
26
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...
12
by: Sensei | last post by:
Hi again! I have recently encountered an old code in K&R style which declared function parameters as register. That puzzles me, and I know this is implementation defined, and depends on...
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.