i=++i; operation undefined?

This is a discussion on i=++i; operation undefined? within the C Programming forums, part of the General Programming Boards category; Hi I know that an object should have its stored value modified at most once between the previous and the ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    i=++i; operation undefined?

    Hi

    I know that an object should have its stored value modified at most once between the previous and the next sequence point. That is the C standard. It means side effects take place in an unspecified order between sequence points. Given a line:
    Code:
    i = ++i;
    This expression statement has two side effects: assignment and increment. But I think operation on 'i' is not undefined. The operation order should look like this:
    1. Increment i
    2. Get i's value
    3. Assign the value to i

    Of course some compilers often just do the first operation, but considering its semantics there seems no other order with this statement. Then why does compiler still report 'operation on i may be undefined'? Only because it has two side effects?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by password636
    Then why does compiler still report 'operation on i may be undefined'? Only because it has two side effects?
    As you noted, the code does not conform to the requirements laid down in the C standard. Besides, I'd say that it is a Good Thing for the compiler to complain, considering that a simple ++i; was all that was intended, so the point is moot anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Technically why wouldn't it 1) get the value of ++i, 2) assign result to i, 3) apply side-effect of increment?

    It is also somewhat simpler to express your idea with simply ++i;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by anon View Post
    Technically why wouldn't it
    1) get the value of ++i,
    2) assign result to i,
    3) apply side-effect of increment?
    I'm sorry but is this possible? I think the side effect of increment is the "INC" directive in assembly, and as the C requires: "INC" should take place before its value is noted. Then why 1) does ++i while 3) still does it again?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by password636 View Post
    I'm sorry but is this possible? I think the side effect of increment is the "INC" directive in assembly, and as the C requires: "INC" should take place before its value is noted. Then why 1) does ++i while 3) still does it again?
    The reason the specification is such as it is, is that it allows compilers to be "clever" about what instruction it uses - not all processors have a specific "inc" instruction - in fact MANY processors don't: 29k, 68k, arm, VAX at least do not have a specific instruction called "inc" - they obviously have other instructions that will do that work (e.g. add with constant 1).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by laserlight View Post
    As you noted, the code does not conform to the requirements laid down in the C standard. Besides, I'd say that it is a Good Thing for the compiler to complain, considering that a simple ++i; was all that was intended, so the point is moot anyway.
    Yes. The compiler reports what doesn't conform to the C standard. I thought if the C standard didn't define the order, there must be more than one order technically, depending on the compilers. But with this example, I can't find another order. So "undefined by the C standard" doesn't mean "more than one order" definitely exists. It's just "undefined". If this is right, I'd say what I thought was wrong.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by password636 View Post
    Yes. The compiler reports what doesn't conform to the C standard. I thought if the C standard didn't define the order, there must be more than one order technically, depending on the compilers. But with this example, I can't find another order. So "undefined by the C standard" doesn't mean "more than one order" definitely exists. It's just "undefined". If this is right, I'd say what I thought was wrong.
    I think you are right in the sense that there is no "alternatives" in this particular case. The case in the standard that covers this particular example ALSO covers i++ + i++, in which case there are definitely more than one alternative interpretation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by matsp View Post
    I think you are right in the sense that there is no "alternatives" in this particular case. The case in the standard that covers this particular example ALSO covers i++ + i++, in which case there are definitely more than one alternative interpretation.
    On processors that don't have "inc" you mentioned above, I wonder if there may be a possibility like this:
    1. Move i's value to a register and add 1 to the register
    2. Use the value in the register to assign i <-- side effect
    3. add 1 to i <-- side effect

    This seems a little stupid. But this sequence can have another order: 1, 3, 2. And thus operations on i undefined. So do the prefix ++ in C guarantee the subsequent operations use the value stored in i or the value obtained through a temporary place, like a register? I mean increment operators definitely will increment the object at the end. But the value used in subsequent operations comes from the object itself or another place where holds [its value + 1]? I think the C language does not define this, it's so machine-related. Besides, the sequence is like anon's.
    Last edited by password636; 04-16-2009 at 09:37 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I still think it's more a case of "this is undefined in the same set as many other things", and the REASON the "many things" are undefined is to allow the compiler to do clever things and not have to worry about "what if the expression is x = i++ + ++i?". This expression, that could probably be defined would just end up being a exception case of "all of these are undefind except when you do i = ++i;" - and the benefit of having exceptions like that would be?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    and the benefit of having exceptions like that would be?
    More developers employed to help write the compiler?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    More developers employed to help write the compiler?
    No, it would only take someone clever about 15 minutes to add an exception to the "give warnings for undefined stuff" - it's not like the compiler doesn't actually DO anything for these undefined things - it's just not guaranteed to do anything like what you may consider "the right thing" [whatever that may be].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21