Thread: a[i]= i++ is undefined while i=i+1 is not

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    15

    a[i]= i++ is undefined while i=i+1 is not

    Hello friends
    Can someone explain me why
    a[i]=i++ is undefined and i=i+1 is legal.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The first is not actually undefined. I suspect it is unspecified (the words unspecified and undefined have different meanings).

    Anyway, why don't YOU explain why you think one is undefined/unspecified and why the other is not? Then people will give you advice as to whether you are right or wrong. You will learn more that way.

    This looks too much like a homework exercise, and this site has a policy that amounts to "do your own homework". So, put in the required effort, and you may get help.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    The first is not actually undefined. I suspect it is unspecified (the words unspecified and undefined have different meanings).
    No, the behaviour is undefined:
    Quote Originally Posted by C++11 Clause 1.9 Paragraph 12b
    Evaluation of an expression (or a sub-expression) in general includes both value computations (including determining the identity of an object for glvalue evaluation and fetching a value previously assigned to an object for prvalue evaluation) and initiation of side effects.
    Quote Originally Posted by C++ Clause 1.9 Paragraph 15d
    If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.
    Of course, with assumptions about what are the types of the objects involved
    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. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    In short, you cannot modify and use a memory location in the same expression (or unsequenced operation), except when the old value is used to compute the new value.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    15
    I think because in first statement compiler is confused whether to store value of i++ in old a[i] or updated a[i+1].Is this the right reason.what is the difference between undefined and unspecified.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I just used this example to nerd-snipe my co-workers. it was glorious!
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I've solved the problem! It's very elegant. </sarcasm>

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int t, i = 0;
        int a[10];
        
        while (i < 10)
            a[t] = i++, t = i;
    
        i = 0;
        while (i < 10)
            printf("[%d]\n", a[i++]);
        
        return 0;
    }

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I've solved the problem! It's very elegant.
    O_o

    You've posted source that has a significant bug.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by phantomotap View Post
    O_o

    You've posted source that has a significant bug.

    Soma
    Can you give me a hint?

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Can you give me a hint?
    O_o

    You use an uninitialized variable.

    [Edit]
    o_O

    Well, I guess I can't.
    [/Edit]

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by phantomotap View Post
    You use an uninitialized variable (`t').
    Yes, I saw that as soon as I hit submit. Would you believe I left that bug there as a test to catch the unwary? No, didn't think so. Oh well.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Would you believe I left that bug there as a test to catch the unwary?
    O_o

    Yes. That is obviously exactly what occurred.

    Why should I believe any other thing?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this undefined?
    By carrotcake1029 in forum C Programming
    Replies: 3
    Last Post: 01-28-2009, 11:42 AM
  2. undefined reference
    By Axel in forum C Programming
    Replies: 16
    Last Post: 09-11-2005, 09:42 PM
  3. Undefined reference
    By Buckshot in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 03:28 PM
  4. undefined What!!
    By breed in forum C Programming
    Replies: 6
    Last Post: 01-22-2002, 05:25 PM
  5. Undefined
    By {Dnw} in forum C Programming
    Replies: 2
    Last Post: 08-27-2001, 10:30 PM