missing return

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carlos Martinez Garcia

    missing return

    Hi all:

    I have a piece of code similar to this:

    bool MyClass::myMeth od() {
    ... (there is no return)
    }

    I have a random behaviour. It depends on if I'm debuging the code or
    not. When I'm debuging it returns false, but in normal execution it
    returns true.

    I have no error/warnings when compile it. ¿Is it the right behaviour?

    Thanks in advance
  • Neelesh Bodas

    #2
    Re: missing return

    Carlos Martinez Garcia wrote:[color=blue]
    > Hi all:
    >
    > I have a piece of code similar to this:
    >
    > bool MyClass::myMeth od() {
    > ... (there is no return)
    > }[/color]

    A function declared to return a value of specific type must return a
    value of that type. Else it is undefined what value would be returned.
    The only exception is "main" which implicitly returns 0.

    Comment

    • Ron Natalie

      #3
      Re: missing return

      Neelesh Bodas wrote:[color=blue]
      > Carlos Martinez Garcia wrote:[color=green]
      >> Hi all:
      >>
      >> I have a piece of code similar to this:
      >>
      >> bool MyClass::myMeth od() {
      >> ... (there is no return)
      >> }[/color]
      >
      > A function declared to return a value of specific type must return a
      > value of that type. Else it is undefined what value would be returned.
      > The only exception is "main" which implicitly returns 0.
      >[/color]
      It's not only undefined what value might be returned, it's undefined
      behavior period. While small things like bools are probably returned
      via registers and just result in indeterminate values, things that the
      compiler has to do more involved copy semantics for (like returning
      classes) might cause the application to blow up.

      Comment

      • Sigmathaar

        #4
        Re: missing return

        When programming a multithread application sometimes there are problems
        in the synchronisation of each thread. Most of the type a bad
        synchornisation may result on a missing return code or a return without
        any sense. For further aid you may post the code you are working at so
        we can see what the problem is.

        Comment

        • Sigmathaar

          #5
          Re: missing return

          If you are programming a multithread application you may obtain a
          return ucode sing the debug feature of your compiler because using the
          debugger may give the thread the time for doing whatever it's supposed
          to do.

          Comment

          • Howard

            #6
            Re: missing return


            "Sigmathaar " <sigmathaar@gma il.com> wrote in message
            news:1133193889 .257810.327160@ z14g2000cwz.goo glegroups.com.. .[color=blue]
            > When programming a multithread application sometimes there are problems
            > in the synchronisation of each thread. Most of the type a bad
            > synchornisation may result on a missing return code or a return without
            > any sense. For further aid you may post the code you are working at so
            > we can see what the problem is.
            >[/color]

            Please quote the text of the message you're responding to.

            The OP stated that there was no return (statement) for the function that was
            declared to return a bool. That makes the code exhibit undefined behavior.
            Threading is not relevant to the issue. If you declare a function to return
            a value, then it must in fact return a value via a return statement.

            -Howard



            Comment

            • Dervish

              #7
              Re: missing return

              >I have no error/warnings when compile it
              Check for compiler warning level. You should get either Error: function
              does not return, or Warning: not all control paths return a value.

              Other possibility - you use really ancient compiler.
              [color=blue]
              >but in normal execution it returns true[/color]
              Better to say: it returns true with some probability. Most likely
              probability = 1/256.

              Comment

              • izida9@gmail.com

                #8
                Re: missing return

                >better to say: it returns true with some probability. Most likely probability = 1/256.

                The probability is 255/256 since every value different from zero is
                interpreted as "true".

                Comment

                Working...