 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dmitriy Iassenev Guest
|
Posted: Thu Oct 02, 2003 4:55 pm Post subject: to guru : strange C++ operator behaviour |
|
|
hi,
I found an interesting thing in operator behaviour in C++ :
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.
Then I tried another example
std::vector<int> a;
a.push_back(1);
a.push_back(2);
std::vector<int>::iterator i = a.begin();
printf("%d",*i++ + *i++);
Different compilers print different values : 2 or 3, thought it _must_ be 3.
Possibly, it is because of the compilers C compatibility, since iterators
are C++ construction and their behaviour therefore suits C++ standards,
while for the basic types compilers guarantee C behaviour.
Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);
Why do all the compilers incorrectly compute the expressions?
Many thanks in advance,
Dmitriy Iassenev.
|
|
| Back to top |
|
 |
WW Guest
|
Posted: Thu Oct 02, 2003 5:01 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
Dmitriy Iassenev wrote:
| Quote: | hi,
I found an interesting thing in operator behaviour in C++ :
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all
the compilers I tested print 2.
|
You think wrong. The above is undefined behavior. You try to change the
value of a variable more than once with an intervening sequence point. The
compiler is allowed to do whatever it wants, including formatting your
harddisk. Search for sequence point i+++++i on Google. One page is:
http://www.embedded.com/story/OEG20020625S0041
--
WW aka Attila
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Thu Oct 02, 2003 5:02 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
| Quote: |
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.
Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);
Why do all the compilers incorrectly compute the expressions?
|
His statement agrees with your test perfectly, doesn't it? You get back 2,
which is 1+1, and after that value is computed, then i gets incremented
(twice). Using his notation,
y = x++ + x++; would be like
y = (t = x+x, x+=1, x+= 1, t);
Right?
-Howard
|
|
| Back to top |
|
 |
tom_usenet Guest
|
Posted: Thu Oct 02, 2003 5:03 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
On Thu, 2 Oct 2003 19:55:37 +0300, "Dmitriy Iassenev"
<iassenev (AT) gsc-game (DOT) kiev.ua> wrote:
| Quote: | hi,
I found an interesting thing in operator behaviour in C++ :
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.
|
This is an FAQ:
http://www.eskimo.com/~scs/C-faq/s3.html
Tom
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Thu Oct 02, 2003 5:05 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
| Quote: | You think wrong. The above is undefined behavior. You try to change the
value of a variable more than once with an intervening sequence point.
The
compiler is allowed to do whatever it wants, including formatting your
harddisk. Search for sequence point i+++++i on Google. One page is:
|
Oh, good point. I forgot about sequence points.
But regardless of what "undefined behavior" means in the standard, you had
best not sell me a compiler that formats my hard drive if I screw up simple
code like this...I know a good lawyer! :-)
-Howard
|
|
| Back to top |
|
 |
Fao, Sean Guest
|
Posted: Thu Oct 02, 2003 5:06 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> wrote
| Quote: |
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.
Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);
Why do all the compilers incorrectly compute the expressions?
His statement agrees with your test perfectly, doesn't it? You get back
2,
which is 1+1, and after that value is computed, then i gets incremented
(twice). Using his notation,
y = x++ + x++; would be like
y = (t = x+x, x+=1, x+= 1, t);
Right?
|
Wrong. modifying the same variable twice in the same expression in
undefined so anything could happen.
|
|
| Back to top |
|
 |
Fao, Sean Guest
|
Posted: Thu Oct 02, 2003 5:08 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
"Dmitriy Iassenev" <iassenev (AT) gsc-game (DOT) kiev.ua> wrote
| Quote: | hi,
I found an interesting thing in operator behaviour in C++ :
int i=1;
printf("%d",i++ + i++);
|
http://www.eskimo.com/~scs/C-faq/q3.3.html
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 02, 2003 5:08 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> wrote...
| Quote: |
int i=1;
printf("%d",i++ + i++);
I think the value of the expression "i++ + i++" _must_ be 3, but all the
compilers I tested print 2.
Bjarne Stroustrup wrote in his book that
y = ++x; is equivalent to y = (x+=1);
while
y = x++; is equivalent to y = (t=x,x+=1,t);
Why do all the compilers incorrectly compute the expressions?
His statement agrees with your test perfectly, doesn't it? You get back
2,
which is 1+1, and after that value is computed, then i gets incremented
(twice). Using his notation,
y = x++ + x++; would be like
y = (t = x+x, x+=1, x+= 1, t);
Right?
|
Nope. Modifying a value of an object twice between sequence points
causes undefined behaviour. Anything is allowed to happen.
The expression (x++ + x++) is NOT like (t = x+x, x+=2) because there
is NO sequence point in the former, whereas in the latter there is
one at the comma.
Victor
|
|
| Back to top |
|
 |
Dmitriy Iassenev Guest
|
Posted: Thu Oct 02, 2003 5:17 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
Many thanks to all of you,
I did find an answer in the FAQ that clarified the described situation.
Best regards,
Dmitriy Iassenev.
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Thu Oct 02, 2003 5:35 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> wrote
| Quote: |
You think wrong. The above is undefined behavior. You try to change
the
value of a variable more than once with an intervening sequence point.
The
compiler is allowed to do whatever it wants, including formatting your
harddisk. Search for sequence point i+++++i on Google. One page is:
Oh, good point. I forgot about sequence points.
But regardless of what "undefined behavior" means in the standard, you had
best not sell me a compiler that formats my hard drive if I screw up
simple
code like this...I know a good lawyer!
|
So if I sell you a chain saw, and due to your ignorance
you cut off your hand, you'll sue me? I suppose so.
Only in America. :-)
-Mike
|
|
| Back to top |
|
 |
WW Guest
|
Posted: Thu Oct 02, 2003 5:36 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
Howard wrote:
| Quote: | You think wrong. The above is undefined behavior. You try to
change the value of a variable more than once with an intervening
sequence point. The compiler is allowed to do whatever it wants,
including formatting your harddisk. Search for sequence point
i+++++i on Google. One page is:
Oh, good point. I forgot about sequence points.
But regardless of what "undefined behavior" means in the standard,
you had best not sell me a compiler that formats my hard drive if I
screw up simple code like this...I know a good lawyer!
|
That is outside of the topic of this newsgroup. It belongs to QoI (Quality
of Implementation). ;-)
--
WW aka Attila
|
|
| Back to top |
|
 |
WW Guest
|
Posted: Thu Oct 02, 2003 5:37 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
Fao, Sean wrote:
| Quote: | Wrong. modifying the same variable twice in the same expression in
undefined so anything could happen.
|
Yeah. Last time I have tried I have laid an egg and a politician told the
truth on national TV. ;-)
--
WW aka Attila
|
|
| Back to top |
|
 |
Mark Kerns Guest
|
Posted: Thu Oct 02, 2003 6:21 pm Post subject: Re: to guru : strange C++ operator behaviour |
|
|
| Quote: | So if I sell you a chain saw, and due to your ignorance
you cut off your hand, you'll sue me?
|
Of course. First I'd sue you, pay my lawyer, declare bankruptcy, and avoid
your counter-suit. This is America after all.
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Fri Oct 03, 2003 5:13 pm Post subject: [OT] Re: to guru : strange C++ operator behaviour |
|
|
"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote
| Quote: |
"Howard" <alicebt (AT) hotmail (DOT) com> wrote in message
news:blhlt1$qot (AT) dispatch (DOT) concentric.net...
But regardless of what "undefined behavior" means in the standard, you
had
best not sell me a compiler that formats my hard drive if I screw up
simple
code like this...I know a good lawyer! :-)
So if I sell you a chain saw, and due to your ignorance
you cut off your hand, you'll sue me? I suppose so.
Only in America. :-)
-Mike
|
:-)
Funny, but a bit ludicrous, Mike. If I buy a chain saw, I know it has the
capability to cut off my hand (or anything else) if I abuse it. After all,
it is DESIGNED to cut things off! However, writing a compiler that
translates the standard's meaing of "undefined behavior" into "I can format
his hard drive if I want" is an act of malice, or at least negligence, and
would definitely be actionable in a court of law (and probably not just in
the US either). Do you write programs that, if the user does not read and
understand your user's manual correctly, will format their hard drive? If
so, please let me know what products you produce so I can avoid them! :-)
As a side note, the reason I brought this up is that that statement about
formatting someone's hard drive when undefined behavior is invoked is used
quite often in this newsgroup, but is, in my opinion, very misleading. The
standard may state that a specific type of coding constitutes undefined
behavior under the standard, but that does NOT mean anyone actually writes
compilers that take malicious or bizarre actions under those circumstances.
Compiler writers have customers that must be satisfied, and some (if not
all) of those customers are BOUND to make mistakes sometimes. It is an
imperative that they take at least "reasonable" actions when undefined
behavior is encountered (assuming that they understand and can detect those
conditions in the first place). Failure to do so will lose them customers,
and money, FAST! (Now, if there are any compiler WRITERS out there that
disagree, please let us know. )
How about, when telling posters about the meaning of "undefined behavior",
we leave it at that, and don't insinuate (or outright state) that they're
going to lose their hard drive if they make a mistake, ok?
-Howard
|
|
| Back to top |
|
 |
Kevin Goodsell Guest
|
Posted: Fri Oct 03, 2003 5:46 pm Post subject: Re: [OT] Re: to guru : strange C++ operator behaviour |
|
|
Howard wrote:
| Quote: | Funny, but a bit ludicrous, Mike. If I buy a chain saw, I know it has the
capability to cut off my hand (or anything else) if I abuse it. After all,
it is DESIGNED to cut things off! However, writing a compiler that
translates the standard's meaing of "undefined behavior" into "I can format
his hard drive if I want" is an act of malice, or at least negligence,
|
What you apparently fail to realize is that the compiler *cannot* detect
undefined behavior in general, and thus cannot assign behavior to it,
nor can a compiler control what happens in the computer system when a
program attempts to do something it shouldn't. Consider DOS systems with
no memory protection. What happens if you overwrite a random memory area
with a random value? Just about anything is possible, as with undefined
behavior in general.
According to Richard Heathfield in _C Unleashed_, page 68-69:
| Quote: |
Nasal Demons
The two most dramatic results of undefined behavior I have personally
witnessed both involved leaving insufficient room in a char array for
the terminating null character of a string.
The first occasion was in 1989, and the victim was my brother, Steve. He
(ahem) drew my attention to his screen, which was displaying a message
asking him to confirm that he wanted to format his hard disk. He was
lucky -- he was asked.
A year or so later, a colleague of mine (hi, Kevin) wasn't quite so
lucky. The first sign of his program's undefined behavior was that his
machine hung. The second sign was that it wouldn't reboot! He had to
spend quite some time nursing the machine back to health using the
diagnostic floppies supplied by the manufacturer.
I've never had anything that bad happen to me. In a way, I've been
fortunate. I would love to bring you a really embarrassing first-hand
account of how I brought down a major government by dereferencing a NULL
pointer or flooded a city by passing (UCHAR_MAX + 1) to isupper(). My
own bugs have been far less dramatic. Sorry to disappoint you.
|
<snip>
| Quote: |
As a side note, the reason I brought this up is that that statement about
formatting someone's hard drive when undefined behavior is invoked is used
quite often in this newsgroup, but is, in my opinion, very misleading.
|
It's not even remotely misleading. According to the standard it is
perfectly possible and permissible. Real life is off-topic here (but
note, from the quote above, that this *does* happen in real life).
| Quote: | The
standard may state that a specific type of coding constitutes undefined
behavior under the standard, but that does NOT mean anyone actually writes
compilers that take malicious or bizarre actions under those circumstances.
|
Never was the claim.
| Quote: | Compiler writers have customers that must be satisfied, and some (if not
all) of those customers are BOUND to make mistakes sometimes. It is an
imperative that they take at least "reasonable" actions when undefined
behavior is encountered (assuming that they understand and can detect those
conditions in the first place).
|
Which they cannot.
| Quote: | Failure to do so will lose them customers,
and money, FAST! (Now, if there are any compiler WRITERS out there that
disagree, please let us know. )
How about, when telling posters about the meaning of "undefined behavior",
we leave it at that, and don't insinuate (or outright state) that they're
going to lose their hard drive if they make a mistake, ok?
|
They could. We shouldn't warn them? They're free to weigh the risk
themselves.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|