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. General Programming
  3. C / C++ / MFC
  4. Short-circuit evaluation questions.

Short-circuit evaluation questions.

Scheduled Pinned Locked Moved C / C++ / MFC
c++comtoolsquestion
4 Posts 4 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 Offline
    R Offline
    rbid
    wrote on last edited by
    #1

    Take a look on the following code:

    if ((A) && (B) && (C))
    {
       ...
    }
    

    If A evaluates to FALSE Will B or C be evaluated? Hint: For built-in types A, B and C, the C++ language description by Bjarne Stroustrup defines that B and C will not be evaluated if A is FALSE. What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? What about compilers that do optimizations, or their optimizations have been disabled? What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant? -- **Ricky Marek** (_AKA: rbid_) -- "Things are only impossible until they are not" --- Jean-Luc Picard My articles

    B R S 3 Replies Last reply
    0
    • R rbid

      Take a look on the following code:

      if ((A) && (B) && (C))
      {
         ...
      }
      

      If A evaluates to FALSE Will B or C be evaluated? Hint: For built-in types A, B and C, the C++ language description by Bjarne Stroustrup defines that B and C will not be evaluated if A is FALSE. What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? What about compilers that do optimizations, or their optimizations have been disabled? What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant? -- **Ricky Marek** (_AKA: rbid_) -- "Things are only impossible until they are not" --- Jean-Luc Picard My articles

      B Offline
      B Offline
      Blake Miller
      wrote on last edited by
      #2

      I believe that the C language specifies that the short-circuit will always take place so that you can DEPEND upon this behavior to avoid problems. Consider this simple function (which is in our code now...) BOOL IsEmpty(char* string) { if( string && string[0] ){ return FALSE; } return TRUE; } I am relying upon the short circuit test of string as a pointer to be NON-NULL and the test failing, otherwise I might try to dereference memory I don't own! (Dereferencing a memory address at zero which will lead to the fatal 'access violation' exception on Windows)

      1 Reply Last reply
      0
      • R rbid

        Take a look on the following code:

        if ((A) && (B) && (C))
        {
           ...
        }
        

        If A evaluates to FALSE Will B or C be evaluated? Hint: For built-in types A, B and C, the C++ language description by Bjarne Stroustrup defines that B and C will not be evaluated if A is FALSE. What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? What about compilers that do optimizations, or their optimizations have been disabled? What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant? -- **Ricky Marek** (_AKA: rbid_) -- "Things are only impossible until they are not" --- Jean-Luc Picard My articles

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        rbid wrote: If A evaluates to FALSE Will B or C be evaluated? No. This is ALWAYS the case. rbid wrote: What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? Yes. rbid wrote: What about compilers that do optimizations, or their optimizations have been disabled? This is nothing to do with optimisations. It's in the standard. rbid wrote: What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant? Yes, it's in the C standard.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        1 Reply Last reply
        0
        • R rbid

          Take a look on the following code:

          if ((A) && (B) && (C))
          {
             ...
          }
          

          If A evaluates to FALSE Will B or C be evaluated? Hint: For built-in types A, B and C, the C++ language description by Bjarne Stroustrup defines that B and C will not be evaluated if A is FALSE. What if A, B, C are macros that expand to complex functions that also do some assignments as side-effects, will this be true also? What about compilers that do optimizations, or their optimizations have been disabled? What about plain 'C' code, Do short-cirtuit rule mentioned above is relevant? -- **Ricky Marek** (_AKA: rbid_) -- "Things are only impossible until they are not" --- Jean-Luc Picard My articles

          S Offline
          S Offline
          Steen Krogsgaard
          wrote on last edited by
          #4

          Be aware that if you define your own overloaded operators &&, || etc. that they will NOT short-circuit! Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

          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