Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Unusual operator behavior

Reply
Thread Tools

Unusual operator behavior

 
 
Karlo Basic
Guest
Posts: n/a
 
      11-10-2003
Greetings!
I'm wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don't know what's going on with
the evaluation of the p variable. I am really interested on how it
works. Thank you in advance,
Karlo.

 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-10-2003
Karlo Basic wrote:
> I'm wondering how do the expressions in the following piece of code
> evaluate and why:
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
> int n = 5, p;
> n = n + n++;


This expression modifies the stored value of 'n' more than once.
Undefined behavior.

> cout << "n = " << n << endl;
> n = 5;
> p = n + n++;


This expression modifies 'n' and at the same time reads its old value
for a purpose other than determining its new value. Undefined behavior.

> cout << "p = " << p << endl;
>
> return 0;
> }
> I compiled it both with g++ and bcc32 and the results are identical:
> n = 11
> p = 10
> The first result seems obvious. But I don't know what's going on with
> the evaluation of the p variable. I am really interested on how it
> works.


There's no way to predict or explain how it will work. Both expressions
result in undefined behavior.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-10-2003


Karlo Basic wrote:
>
> Greetings!
> I'm wondering how do the expressions in the following piece of code
> evaluate and why:
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
> int n = 5, p;
> n = n + n++;


modifying a variable twice between sequence points -> undefined behaviour

> cout << "n = " << n << endl;
> n = 5;
> p = n + n++;
> cout << "p = " << p << endl;
>
> return 0;
> }
> I compiled it both with g++ and bcc32 and the results are identical:
> n = 11
> p = 10
> The first result seems obvious.


Not really.

> But I don't know what's going on with
> the evaluation of the p variable. I am really interested on how it
> works.


There is no correct answer. The compiler can do anything it wants.
Your program has produced undefined behaviour and it is pointless
to think about a 'correct solution' since there is none.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Tim Threlfall
Guest
Posts: n/a
 
      11-10-2003
The relevant part of the standard is Section 5 paragraph 4

Except where noted, the order of evaluation of operands of individual
operators and subexpressions of indi-vidual expressions, and the order
in which side effects take place, is unspecified.53) Between the
previous and next sequence point a scalar object shall have its stored
value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the
value to be stored. The requirements of this paragraph shall be met for
each allowable ordering of the subexpressions of a full expression;
otherwise the behavior is undefined. [Example:
i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented
end example]

 
Reply With Quote
 
A
Guest
Posts: n/a
 
      11-11-2003

> I'm wondering how do the expressions in the following piece of code
> evaluate and why:
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
> int n = 5, p;
> n = n + n++;
> cout << "n = " << n << endl;
> n = 5;
> p = n + n++;
> cout << "p = " << p << endl;
>
> return 0;
> }
> I compiled it both with g++ and bcc32 and the results are identical:
> n = 11
> p = 10
> The first result seems obvious. But I don't know what's going on with
> the evaluation of the p variable. I am really interested on how it
> works. Thank you in advance,
> Karlo.


n = 5;
p = n + n++; // EASY! add 5 and 5 and assign it to p and then increment n
(postfix increment)
cout << "p = " << p << endl;

Regards,
A


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-11-2003


A wrote:
>
> > I'm wondering how do the expressions in the following piece of code
> > evaluate and why:
> >
> > #include <iostream>
> > using namespace std;
> >
> > int main()
> > {
> > int n = 5, p;
> > n = n + n++;
> > cout << "n = " << n << endl;
> > n = 5;
> > p = n + n++;
> > cout << "p = " << p << endl;
> >
> > return 0;
> > }
> > I compiled it both with g++ and bcc32 and the results are identical:
> > n = 11
> > p = 10
> > The first result seems obvious. But I don't know what's going on with
> > the evaluation of the p variable. I am really interested on how it
> > works. Thank you in advance,
> > Karlo.

>
> n = 5;
> p = n + n++; // EASY! add 5 and 5 and assign it to p and then increment n
> (postfix increment)


Not at all.
The compiler coud also do.

Take the value of n 5
increment n
Take the value of n 6
Add both numbers

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
Sinora
Guest
Posts: n/a
 
      11-12-2003
Here is what is going on here:

n++ evaluates the expression first and add 1 to it. Therefore

n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
11
p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.

In short,

++n increment the n first
n++ evaluate the expression first

Hope it explained your confusion.
Dev

"Karlo Basic" <(E-Mail Removed)> wrote in message
news:bonjh2$1gcb7p$(E-Mail Removed)-berlin.de...
> Greetings!
> I'm wondering how do the expressions in the following piece of code
> evaluate and why:
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
> int n = 5, p;
> n = n + n++;
> cout << "n = " << n << endl;
> n = 5;
> p = n + n++;
> cout << "p = " << p << endl;
>
> return 0;
> }
> I compiled it both with g++ and bcc32 and the results are identical:
> n = 11
> p = 10
> The first result seems obvious. But I don't know what's going on with
> the evaluation of the p variable. I am really interested on how it
> works. Thank you in advance,
> Karlo.
>



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-12-2003
"Sinora" <(E-Mail Removed)> wrote...
> Here is what is going on here:
>
> n++ evaluates the expression first and add 1 to it. Therefore


Stop right here. Adding of 1 to 'n' does NOT have to happen at the
same time as the insertion of the value of 'n' into the expression.

As others have mentioned, the behaviour of this code is undefined.

>
> n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
> 11
> p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.
>
> In short,
>
> ++n increment the n first
> n++ evaluate the expression first
>
> Hope it explained your confusion.


You are confused no less than the original poster. Read the posts
of others.

> Dev
>
> "Karlo Basic" <(E-Mail Removed)> wrote in message
> news:bonjh2$1gcb7p$(E-Mail Removed)-berlin.de...
> > Greetings!
> > I'm wondering how do the expressions in the following piece of code
> > evaluate and why:
> >
> > #include <iostream>
> > using namespace std;
> >
> > int main()
> > {
> > int n = 5, p;
> > n = n + n++;
> > cout << "n = " << n << endl;
> > n = 5;
> > p = n + n++;
> > cout << "p = " << p << endl;
> >
> > return 0;
> > }
> > I compiled it both with g++ and bcc32 and the results are identical:
> > n = 11
> > p = 10
> > The first result seems obvious. But I don't know what's going on with
> > the evaluation of the p variable. I am really interested on how it
> > works. Thank you in advance,
> > Karlo.
> >

>
>



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-12-2003
"Sinora" <(E-Mail Removed)> wrote...
> Here is what is going on here:
>
> n++ evaluates the expression first and add 1 to it. Therefore


Stop right here. Adding of 1 to 'n' does NOT have to happen at the
same time as the insertion of the value of 'n' into the expression.

As others have mentioned, the behaviour of this code is undefined.

>
> n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
> 11
> p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.
>
> In short,
>
> ++n increment the n first
> n++ evaluate the expression first
>
> Hope it explained your confusion.


You are confused no less than the original poster. Read the posts
of others.

> Dev
>
> "Karlo Basic" <(E-Mail Removed)> wrote in message
> news:bonjh2$1gcb7p$(E-Mail Removed)-berlin.de...
> > Greetings!
> > I'm wondering how do the expressions in the following piece of code
> > evaluate and why:
> >
> > #include <iostream>
> > using namespace std;
> >
> > int main()
> > {
> > int n = 5, p;
> > n = n + n++;
> > cout << "n = " << n << endl;
> > n = 5;
> > p = n + n++;
> > cout << "p = " << p << endl;
> >
> > return 0;
> > }
> > I compiled it both with g++ and bcc32 and the results are identical:
> > n = 11
> > p = 10
> > The first result seems obvious. But I don't know what's going on with
> > the evaluation of the p variable. I am really interested on how it
> > works. Thank you in advance,
> > Karlo.
> >

>
>



 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-12-2003
Sinora wrote:
> Here is what is going on here:
>
> n++ evaluates the expression first and add 1 to it. Therefore
>
> n = n + n++ (= 5 + 5) = 10 after evaluation 1 is added to n therefore n =
> 11
> p = n+n++ (= 5+5) = 10, n is = 6 after this expression but p is still 10.
>
> In short,
>
> ++n increment the n first
> n++ evaluate the expression first
>
> Hope it explained your confusion.


The explanation is incorrect and can only add to the confusion.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ [13.9] intrinsic behavior of operator++(), operator++(int) gob00st@googlemail.com C++ 4 02-20-2009 07:27 PM
Unusual session_end behavior... =?Utf-8?B?SGVucnk=?= ASP .Net 0 01-12-2005 04:35 PM
SoapException - unusual behavior Marty McDonald ASP .Net 1 05-19-2004 03:21 PM
Response.Write's unusual behavior (i think) Farooq Khan ASP .Net 3 10-24-2003 03:00 PM
Re: Unusual behavior by Mailwasher Lord Shaolin Computer Security 4 08-15-2003 09:26 PM



Advertisments