Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 32,304 of 33,346    |
|    Francis Glassborow to ravinder thakur    |
|    Re: postfix operator++    |
|    17 May 12 06:44:50    |
   
   6246f224   
   From: francis.glassborow@btinternet.com   
      
   On 17/05/2012 08:13, ravinder thakur wrote:   
   > { Though this question may seem trivial, it may also be confusing and   
   > tricky in light of the series of relevant changes in the standard,   
   > so hopefully the follow-ups will clear up the issue. -mod }   
   >   
   > hello guys,   
   >   
   > I am kind of puzzled by one simple c++ constuct. What should be the   
   > value of x after the statement:   
   >   
   > int x = 10;   
   > x = x++;   
   >   
   > I expect value of x to be 10 after the second assignment, however its   
   > coming out to be 11. Any ideas whats happening ?   
   >   
   > Thanks!   
   > xoxo   
   >   
      
   You have invoked undefined behaviour because your assignment statement   
   modifies x twice without an intervening sequence point. This is always a   
   no no in C and C++ for built-in types. The same rule applies to   
   struct/class types except that in most instances where it looks as this   
   has been done (2 modifications to same memory between sequence points)   
   there are some hidden sequence points because overloaded operators are   
   actually function calls.   
      
   In a sense you are fortunate in that the undefined behaviour has   
   manifested itself in such a mild fashion whilst alerting you to a   
   problem in your understanding of C++   
      
   Francis   
   >   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca