Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Captain Obvious

Captain Obvious

Scheduled Pinned Locked Moved The Weird and The Wonderful
com
25 Posts 12 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Robert Surtees

    Perhaps I'm being Captain Obvious but, in plain C at least, the result is undefined. The compiler I'm using at the moment does not leave the loop as i is never incremented.

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #9

    With gcc (version 3.4.4) it doesn't work, i remaining 0 forever. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

    L P 2 Replies Last reply
    0
    • C CPallini

      With gcc (version 3.4.4) it doesn't work, i remaining 0 forever. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #10

      i wonder what would happen in other languages?? *writes java application* we've got a Infinite loop here...i=0, all the way :omg: :confused:

      -st0le [st0le'n'stuff softwarez!] http://st0lenc0des.googlepages.com/

      modified on Tuesday, February 19, 2008 12:24 AM

      C 1 Reply Last reply
      0
      • R Robert Surtees

        Perhaps I'm being Captain Obvious but, in plain C at least, the result is undefined. The compiler I'm using at the moment does not leave the loop as i is never incremented.

        B Offline
        B Offline
        Brady Kelly
        wrote on last edited by
        #11

        C# is the same, so, it seems this behaviour is predictable and I will have to read PIEBALD's lengthy excerpt.

        1 Reply Last reply
        0
        • L Lost User

          i wonder what would happen in other languages?? *writes java application* we've got a Infinite loop here...i=0, all the way :omg: :confused:

          -st0le [st0le'n'stuff softwarez!] http://st0lenc0des.googlepages.com/

          modified on Tuesday, February 19, 2008 12:24 AM

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #12

          Java behaves the same way, i.e. loops indefinitely (or at least, until the hammer comes down). :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          1 Reply Last reply
          0
          • R Robert Surtees

            Perhaps I'm being Captain Obvious but, in plain C at least, the result is undefined. The compiler I'm using at the moment does not leave the loop as i is never incremented.

            L Offline
            L Offline
            liquidplasmaflow
            wrote on last edited by
            #13

            What's happening makes sense. The operator in question is the postfix incrementor; it increments the variable, but it returns the pre-incrementation value. So, it increments itself, but it's being assigned its original value.

            J 1 Reply Last reply
            0
            • L liquidplasmaflow

              What's happening makes sense. The operator in question is the postfix incrementor; it increments the variable, but it returns the pre-incrementation value. So, it increments itself, but it's being assigned its original value.

              J Offline
              J Offline
              Jitse
              wrote on last edited by
              #14

              Isn't the question just whether the runtime environment first assigns the old value. I really thought this would just eventually increment the variable. Because it should normally first assign the old value to i, and then increment i. Why should it ever store the old value somewhere first, then increment i, then put that old value back in i? I know I'm wrong, cuz as said before C# gives an infinite loop, but still it's weird.

              L L 2 Replies Last reply
              0
              • D Doc Lobster

                I wrote a test application (the code works in VC++ 8, and i stays 0 in VC#), but it seems that I had a mental blockade to enter i = i++; It took me two i = i+1; until I could force my fingers to do that. Is the result really undefined by ANSI or whoevers specification?

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #15

                The code I wrote to test it is:

                # include <stdio.h>

                int
                main
                (
                int argc
                ,
                char* argv[]
                )
                {
                int result = 0 ;

                result = result++ ;
                
                printf ( "%d" , result ) ;
                
                return ( result ) ;
                

                }

                (This is the same code I used to test Borland C/C++ 5.5) compiling this using DEC C V6.0-001 on OpenVMS Alpha V7.3-2 yields the warning " In this statement, the expression "result=result++" modifies the variable "result" more than once without an intervening sequence point. This behavior is undefined. " but it compiles and returns 1 when executed.

                1 Reply Last reply
                0
                • J Jitse

                  Isn't the question just whether the runtime environment first assigns the old value. I really thought this would just eventually increment the variable. Because it should normally first assign the old value to i, and then increment i. Why should it ever store the old value somewhere first, then increment i, then put that old value back in i? I know I'm wrong, cuz as said before C# gives an infinite loop, but still it's weird.

                  L Offline
                  L Offline
                  liquidplasmaflow
                  wrote on last edited by
                  #16

                  How could it do that? The operator's function has to end (return) before the assignment occurs; that means the incrementation has to happen first.

                  L 1 Reply Last reply
                  0
                  • C CPallini

                    With gcc (version 3.4.4) it doesn't work, i remaining 0 forever. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #17

                    But not so with gcc version 3.2 (mingw special 20020817-1)

                    1 Reply Last reply
                    0
                    • L leppie

                      st0le wrote:

                      The fact that it makes sense

                      Makes sense in the fact that it compiles and does not break anything? ;P

                      xacc.ide - now with IronScheme support
                      IronScheme - 1.0 alpha 1 out now

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #18

                      leppie wrote:

                      it compiles and does not break anything

                      Two big qualities indeed. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                      1 Reply Last reply
                      0
                      • L liquidplasmaflow

                        How could it do that? The operator's function has to end (return) before the assignment occurs; that means the incrementation has to happen first.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #19

                        liquidplasmaflow wrote:

                        the incrementation has to happen first

                        says who? the autoincrement is not necessary in the expression evaluation, and hence it can be scheduled before or after the assignment operator, that is why the net result is undefined. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                        L M 2 Replies Last reply
                        0
                        • J Jitse

                          Isn't the question just whether the runtime environment first assigns the old value. I really thought this would just eventually increment the variable. Because it should normally first assign the old value to i, and then increment i. Why should it ever store the old value somewhere first, then increment i, then put that old value back in i? I know I'm wrong, cuz as said before C# gives an infinite loop, but still it's weird.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #20

                          I agree. If the code would look like this: a = i++; Then a would be assigned the value of i and i would be incremented after that, so I would expect in given case that in first iteration i would be assigned value of 0 then i would be incremented. Weird indeed.

                          1 Reply Last reply
                          0
                          • L Luc Pattyn

                            liquidplasmaflow wrote:

                            the incrementation has to happen first

                            says who? the autoincrement is not necessary in the expression evaluation, and hence it can be scheduled before or after the assignment operator, that is why the net result is undefined. :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                            L Offline
                            L Offline
                            liquidplasmaflow
                            wrote on last edited by
                            #21

                            C++'s operator ++ is a function. Last I checked, a function can't do anything after it returns :) int& operator++(int& argument, int) { int return_value = argument; argument += 1; return return_value; } Am I right?

                            L 1 Reply Last reply
                            0
                            • L liquidplasmaflow

                              C++'s operator ++ is a function. Last I checked, a function can't do anything after it returns :) int& operator++(int& argument, int) { int return_value = argument; argument += 1; return return_value; } Am I right?

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #22

                              Hi, the autoincrement operator may be implemented as a function, I don't think it has to; in C it typically is not. And even when it is a function, it could be inlined automatically, and the instructions then can be rescheduled by the compiler, so there is no way you can predict which one (the final store, or the autoincrement) will occur last and hence prevail; all that in accordance with the legalese language specs PIEBALD showed us. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                              P 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Hi, the autoincrement operator may be implemented as a function, I don't think it has to; in C it typically is not. And even when it is a function, it could be inlined automatically, and the instructions then can be rescheduled by the compiler, so there is no way you can predict which one (the final store, or the autoincrement) will occur last and hence prevail; all that in accordance with the legalese language specs PIEBALD showed us. :)

                                Luc Pattyn [Forum Guidelines] [My Articles]


                                This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                                P Offline
                                P Offline
                                PIEBALDconsult
                                wrote on last edited by
                                #23

                                I'd be interested to know how early versions of C would handle it. I expect the increment should happen first (if indeed it's handled by a hardware instruction), then the original value assigned back, yielding the infinite loop.

                                L 1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  I'd be interested to know how early versions of C would handle it. I expect the increment should happen first (if indeed it's handled by a hardware instruction), then the original value assigned back, yielding the infinite loop.

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #24

                                  I have used dozens of C compilers over the years, most if not all of them did not care about the ambiguity, but simply evaluated the entire expression including side-effects before storing the result. It is only with the advent of RISC and advanced instruction scheduling that things got unclear, and the undefined stuff got introduced. Anyway, I learned long ago not to write code that would be ambiguous or just difficult to read and understand, so it never really mattered ... What is the point of studying a page of rules to make sure something is/isn't ambiguous or undefined, where one could add an intermediate statement or a pair of parentheses? It isn't APL or Lisp after all. :)

                                  Luc Pattyn [Forum Guidelines] [My Articles]


                                  This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                                  1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    liquidplasmaflow wrote:

                                    the incrementation has to happen first

                                    says who? the autoincrement is not necessary in the expression evaluation, and hence it can be scheduled before or after the assignment operator, that is why the net result is undefined. :)

                                    Luc Pattyn [Forum Guidelines] [My Articles]


                                    This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                                    M Offline
                                    M Offline
                                    Megidolaon
                                    wrote on last edited by
                                    #25

                                    It seems assigning the value of an incremented variable to itself overrides the increment operation. If not, it wouldn't actually matter in which order the operations are executed as the result would be the same: increment comes first: i = 0 // i = 0 i++ = 1 // i = 1 i = i // i = 1 result: 1 assignment comes first i = 0 // i = 0 i = i // i = 0 i++ // i = 1 result: 1

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups