473,320 Members | 1,930 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,320 software developers and data experts.

Keyword register

Just some thought on it, wanting to see any explanations.
It was advised in this newsgroups that we should avoid the use of
keyword register.

However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.

Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

So why we should not use register?

An implementation taking it into account means that the keyword helps it
indeed, otherwise the implementation *should* ignore it.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #1
12 2194
* Ioannis Vranos:

Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.


Well, the standard says 'register' is typically ignored if the address
of the object is taken.

That indicates that you can take the address of a 'register' variable.

As far as I can see it does not say anywhere that you can't .

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
Alf P. Steinbach wrote:
Well, the standard says 'register' is typically ignored if the address
of the object is taken.

That indicates that you can take the address of a 'register' variable.

As far as I can see it does not say anywhere that you can't .


Yes, the standard says:

"A register specifier has the same semantics as an auto specifier
together with a hint to the implementation that the object so declared
will be heavily used. [Note: the hint can be ignored and in most
implementations it will be ignored if the address of the object is
taken. —end note]".
In any case, my original question why register should be avoided remains.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #3
* Ioannis Vranos:

In any case, my original question why register should be avoided remains.


Who says it should be avoided?

But as a practical matter, nothing is gained by using it, and
it's more to type and more to parse (for the human reader).

So in practice we don't use it, except when doing a copy-paste
of Duff's device. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4
Ioannis Vranos wrote:
Yes, the standard says:

"A register specifier has the same semantics as an auto specifier
together with a hint to the implementation that the object so declared
will be heavily used. [Note: the hint can be ignored and in most
implementations it will be ignored if the address of the object is
taken. —end note]".

For completeness I will mention here what TC++PL 3 says about register:
"Declaring a variable register is a hint to the compiler to optimize for
frequent access; doing so is redundant with most modern compilers".
Redundant doesn't mean "don't use it".

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
Ioannis Vranos wrote:

Just some thought on it, wanting to see any explanations.

It was advised in this newsgroups that we should avoid the use of
keyword register.

However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.

Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

So why we should not use register?

An implementation taking it into account means that the keyword helps it
indeed, otherwise the implementation *should* ignore it.


As I see it:

The keyword 'register' originated in the dark ages of C, when compiler
and their optimizer weren't that sophisticated as they are today.
In the good old days, 'register' was a hint to the compiler to concentrate
on that variable to keep it as long as possible in a register.
If you use register today, then you are lucky if the compiler simply
ignores it. If you are unlucky, then your guess about variable usage
was wrong and the compiler tries to 'optimize' according to your hint,
instead of relying on its own data flow analysis. And guess: as with
most low level optimizations, the programmer is usually wrong. Let
the compilers data flow analysis figure out which variable to put into
a register. It can do that task better then most humans. Not to mention
that the data flow analysis might figure out, that optimal register usage in
the first half of the function differs significantly from optimal register usage
in the second half of the function. And that's something a simple 'register'
cannot do.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6
Ioannis Vranos wrote:
Just some thought on it, wanting to see any explanations.
It was advised in this newsgroups that we should avoid the use of
keyword register.

However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.

Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

So why we should not use register?

The very short reason is that nowadays register allocation systems are
very clever, and almost certainly the blunt instrument which is
"register" is highly unlikely to help guide the optimiser to better code
than it could construct simply by itself.

Chris
Jul 22 '05 #7
Ioannis Vranos <iv*@remove.this.grad.com> wrote in message news:<1101205775.543355@athnrd02>...

[ ... ]
However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.
Unfortunately, a number of implementations (e.g. at least some of
Borland's compilers) don't ignore it when they should. The result is
that using it can pessimize the code.
Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.
That's true in C, but not in C++.
So why we should not use register?

An implementation taking it into account means that the keyword helps it
indeed, otherwise the implementation *should* ignore it.


It inevitably does at least slight harm to readability and may do
slight harm to efficiency, while standing no chance whatsoever of
doing any good at all.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #8
On Tue, 23 Nov 2004 12:29:45 +0200, Ioannis Vranos
<iv*@remove.this.grad.com> wrote in comp.lang.c++:
Just some thought on it, wanting to see any explanations.
It was advised in this newsgroups that we should avoid the use of
keyword register.

However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.
This is true in C++, but is one of the differenced between C++ and C.
It is not legal in C to take the address of an object defined with the
register storage class, whether the compiler ignores the hint or not.
Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

So why we should not use register?

An implementation taking it into account means that the keyword helps it
indeed, otherwise the implementation *should* ignore it.


You have already gotten a lot of good replies, and in general I agree
with them. In general, a programmer, particularly an applications
level programmer, should not use the register keyword.

It is in fact much like any other source code optimization. It should
not be used at all initially, unless you doing things like writing
hardware-level drivers or interrupt service routines, and probably not
even then.

Once you have a program that operates correctly but more slowly than
desired, you can apply tools like profilers to find out where the
bottlenecks are. Then, and only then, apply source code optimizations
like selective use of the register keyword to see if it improves
performance.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #9
Ioannis Vranos <iv*@remove.this.grad.com> wrote in message news:<1101205775.543355@athnrd02>...
So why we should not use register?


Can you please give some example where usage of keyword register
would be appropriate.

Greetings, Bane.
Jul 22 '05 #10
Karl Heinz Buchegger wrote:
As I see it:

The keyword 'register' originated in the dark ages of C, when compiler
and their optimizer weren't that sophisticated as they are today.
In the good old days, 'register' was a hint to the compiler to concentrate
on that variable to keep it as long as possible in a register.
If you use register today, then you are lucky if the compiler simply
ignores it.

If there is no benefit of its use in a platform, then an implementation
should ignore it. After all, it is a language feature.

If you are unlucky, then your guess about variable usage
was wrong

Why? For example why an indexing variable in a loop would be a wrong use?

and the compiler tries to 'optimize' according to your hint,
instead of relying on its own data flow analysis. And guess: as with
most low level optimizations, the programmer is usually wrong.

Do you think the same for inline keyword?
Let
the compilers data flow analysis figure out which variable to put into
a register. It can do that task better then most humans. Not to mention
that the data flow analysis might figure out, that optimal register usage in
the first half of the function differs significantly from optimal register usage
in the second half of the function. And that's something a simple 'register'
cannot do.

Yes, in this case the implementation should ignore the keyword register
(as all compilers of mine do), otherwise a compiler lacking such a "data
flow analysis" would benefit from the keyword.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #11
Jerry Coffin wrote:
while standing no chance whatsoever of
doing any good at all.

Not even in some embedded system?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #12
Branimir Maksimovic wrote:
Can you please give some example where usage of keyword register
would be appropriate.

Perhaps in a for loop indexing variable in a Z80 embedded system
dedicated application?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #13

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

Similar topics

9
by: Jackie | last post by:
Hi everyone, Does anyone know when "register" declarations should be used and when "register" must not be used? If possible please give examples for both cases. Thanks
13
by: Dave win | last post by:
howdy.... plz take a look at the following codes, and tell me the reason. 1 #define swap(a,b) a=a^b;b=b^a;a=a^b 2 3 int main(void){ 4 register int a=4; 5 register int b=5;...
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...
3
by: Sean DiZazzo | last post by:
Why is the following not working? Is there any way to get keyword arguments working with exposed XMLRPC functions? ~~~~~~~~~~~~~~~~ server.py import SocketServer from SimpleXMLRPCServer import...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.