Question about increment..

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about increment..

Mani
Hi,

I recently to try some toy things in DevC++ (version I am using is 4.9.9.2)

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Question about increment..

Mani
Hi,

I recently to try some toy things in DevC++ (version I am using is 4.9.9.2)

The code I had was this:

    int a = 1, b = 2;
    int c = a++ + b++ + a + b;

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;

I expected the c value to be 8 (by operator precedence, ++ has higher
precedence than +, so do in order:
a++, then b++
then do (a++ + b++)
then add the previous result and a
then add the previous result and b

but the answer I got was 6..

what might be the reason for this..??

thanks, murali

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about increment..

Kai Tietz-2
Hi,

The answer is that your code describes undefined-behavior for C (C++)
language.  If you take a closer look to C specification, you will
learn that the pre/post-increment|decrement operator has to be
executed before next sequence-point.  Within your calculation, the
sequence-point is the assignment operator, so order of execution of
your code is undefined.

Regards,
Kai

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about increment..

lrn-2
In reply to this post by Mani
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 18.09.2012 20:28, Mani wrote:

> Hi,
>
> I recently to try some toy things in DevC++ (version I am using is
> 4.9.9.2)
>
> The code I had was this:
>
> int a = 1, b = 2; int c = a++ + b++ + a + b;
>
> cout << "a = " << a << endl; cout << "b = " << b << endl; cout <<
> "c = " << c << endl;
>
> I expected the c value to be 8 (by operator precedence, ++ has
> higher precedence than +, so do in order: a++, then b++ then do
> (a++ + b++) then add the previous result and a then add the
> previous result and b
>
> but the answer I got was 6..
>
> what might be the reason for this..??
>
It's called "undefined behaviour" [1].
There's a good article [2] about this. Sadly, it's in Russian, but
there's an image [3] in that article that neatly illustrates the point.

[1] http://en.wikipedia.org/wiki/Undefined_behavior
[2] http://lurkmore.to/%2B%2Bi_%2B_%2B%2Bi
[3] http://lurkmore.so/images/9/95/Cpp.png

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iQEcBAEBAgAGBQJQWK1aAAoJEOs4Jb6SI2CwnJwIAILS04mfqryi388gD89MsNHQ
t9CzoeDmlPri0CD6SkXBu/rn7IEoWGS135S2MJJBQGPdH2oyiR7xGmF1Fr01hBxq
NGPZ46cqA0BIUxeQINH5C6Xm1Og0S7PJiN+UDmXk3flquFWLoNMj1CtjRgDYPND6
k+lIOJMvD3THpA/i2s2gvVFlJcIUMfe+i0Wel3kbqNSH0gC9FXcV0u2VIr5M0fOa
+3Zd4vTuHfKt8y1krIvtqSZjP253XTzOz9nljDyiWj7U7tAzBu2FXzE9EnyYMrHT
nV3IhS3LeM5KATjZN1LoVcau0XDdw4KN3ngsasndlG+KdCHZTpFeWWIZdto8Yc0=
=E4KN
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about increment..

Keith Marshall
In reply to this post by Kai Tietz-2
On 18/09/12 17:48, Kai Tietz wrote:
> The answer is that your code describes undefined-behavior for C (C++)
> language.  If you take a closer look to C specification, you will
> learn that the pre/post-increment|decrement operator has to be
> executed before next sequence-point.

This is correct.

> Within your calculation, the sequence-point is the assignment operator,

This is incorrect.  The OP's original expression:

     int c = a++ + b++ + a + b;

has only one sequence point -- the terminal semicolon.

> so order of execution of your code is undefined.

And again, correct.  The sequence is complete when the assignment to
variable c and the two side effects of post-incrementing variables a and
b have all been completed.  However, each of the three effects is
independent of the other two, and the order in which they must be
completed is undefined.

--
Regards,
Keith.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about increment..

Kai Tietz-2
2012/9/18 Keith Marshall <[hidden email]>:

> On 18/09/12 17:48, Kai Tietz wrote:
>> The answer is that your code describes undefined-behavior for C (C++)
>> language.  If you take a closer look to C specification, you will
>> learn that the pre/post-increment|decrement operator has to be
>> executed before next sequence-point.
>
> This is correct.
>
>> Within your calculation, the sequence-point is the assignment operator,
>
> This is incorrect.  The OP's original expression:
>

This is correct.  Please see in Annex C the following description.
 ... "— The end of a full expression: an initializer (6.7.8); the
expression in an expression
statement (6.8.3); the controlling expression of a selection statement
(if or switch)
(6.8.4); the controlling expression of a while or do statement
(6.8.5); each of the
expressions of a for statement (6.8.5.3); the expression in a return statement
(6.8.6.4)."

>      int c = a++ + b++ + a + b;

We have here an initializer (6.7.8).

> has only one sequence point -- the terminal semicolon.

Well, the terminal semicolon itself isn't a sequence-point, but of
course it describes the end of the expression.

Kai

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about increment..

Kai Tietz-2
Forget about initializers ... misread it.

Correct, the sequence-point here is the expression itself, which is
terminated by the semicolon.

Kai

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Question about increment..

Mani
In reply to this post by Mani
Hi,

I was wondering if you had any comments as to how we can explain the
below.. thanks, murali.


---------- Forwarded message ----------

Hi,

I recently to try some toy things in DevC++ (version I am using is 4.9.9.2)

The code I had was this:

    int a = 1, b = 2;
    int c = a++ + b++ + a + b;

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;

I expected the c value to be 8 (by operator precedence, ++ has higher
precedence than +, so do in order:
a++, then b++
then do (a++ + b++)
then add the previous result and a
then add the previous result and b

but the answer I got was 6..

what might be the reason for this..??

thanks, murali

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://ad.doubleclick.net/clk;258768047;13503038;j?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about increment..

Earnie Boyd
On Thu, Sep 20, 2012 at 12:53 PM, Mani wrote:
> Hi,
>
> I was wondering if you had any comments as to how we can explain the
> below.. thanks, murali.
>

This was responded to by many, are you receiving list mail?

--
Earnie
-- https://sites.google.com/site/earnieboyd

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://ad.doubleclick.net/clk;258768047;13503038;j?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about increment..

Mani
thanks for pointing out.. I had not received the original responses..
Probably I changed my settings..
In any case, thanks for all the responses.. they were very helpful..

best, murali.

On Thu, Sep 20, 2012 at 2:06 PM, Earnie Boyd
<[hidden email]> wrote:

> On Thu, Sep 20, 2012 at 12:53 PM, Mani wrote:
>> Hi,
>>
>> I was wondering if you had any comments as to how we can explain the
>> below.. thanks, murali.
>>
>
> This was responded to by many, are you receiving list mail?
>
> --
> Earnie
> -- https://sites.google.com/site/earnieboyd

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://ad.doubleclick.net/clk;258768047;13503038;j?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe