Thread: wat is the logic behind this?

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vpshastry View Post
    i mean i=i++ can't be divided as i=i and ++i
    Why?


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The problem is that exactly when the increment occurs is not specified. We only know that the increment occurs at some point AFTER the initial value of i is loaded on the RHS. So there are two possibilities:

    The original value of i is stored in a temporary, then i is incremented, then the temporary is stored in i. The result is no change in the value whatsoever.

    The original value of i is stored in a temporary, then the temporary is stored in i, then i is incremented. The result is that i is incremented.

    I believe this is an example of unspecified behavior, as opposed to undefined.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, I think that the behaviour is undefined. Consider: we know that between the previous and next sequence point, a value will be assigned to i, thus i will be modified. We also know that "side effect of updating the stored value of the operand shall occur between the previous and the next sequence point". Therefore, i is modified twice between consecutive sequence points, resulting in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by vpshastry View Post
    i mean i=i++ can't be divided as i=i and ++i
    Well, try to come up with one example where i=i; could be useful.

    It adds nothing to the increment operation on it's own, and it's behaviour is apparently undefined, so it's best avoided it seems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Handling logic and draw cycles
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 07-25-2009, 10:15 AM
  2. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  3. wat is the use of sizeof(int)?
    By zell in forum C Programming
    Replies: 3
    Last Post: 01-24-2005, 05:27 AM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM