Strange Behaviour initializing bool

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crispin
    New Member
    • Jan 2007
    • 11

    Strange Behaviour initializing bool

    Hi,

    I was under the impression that bool's were false by default... Am I wrong about this?

    In VC++6, when I define a bool in a class header (i.e. bool testbool) and then access it BEFORE I have initialized it to a definite value, it returns 1. I am sure that I have not accidentally initialized it to true anywhere. Just curious, is it supposed to be this way?

    Cheers,
    Crispin
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by crispin
    Hi,

    I was under the impression that bool's were false by default... Am I wrong about this?

    In VC++6, when I define a bool in a class header (i.e. bool testbool) and then access it BEFORE I have initialized it to a definite value, it returns 1. I am sure that I have not accidentally initialized it to true anywhere. Just curious, is it supposed to be this way?

    Cheers,
    Crispin
    C/C++ considered any number not 0 to be 1, and will return that value unless the memory location it allocates to the boolean value is all 0's, which is not likely.

    Basically - it creates the space for the variable in memory, and that space holds whatever value is there, before you initialize it. Once you initialize it, that value is wiped out, but before you do, you are accessing memory that is part of some other program or file's temp space, that is now free.

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      Right. This is the case with all variables in c++. Unless you give them an initial value you are left with the garbage at that particular memory address.

      Comment

      • crispin
        New Member
        • Jan 2007
        • 11

        #4
        Perfect - This explains a lot. Thanks for your advice :)

        Comment

        • vishwanathntandasi
          New Member
          • Feb 2007
          • 1

          #5
          Originally posted by crispin
          Hi,

          I was under the impression that bool's were false by default... Am I wrong about this?

          In VC++6, when I define a bool in a class header (i.e. bool testbool) and then access it BEFORE I have initialized it to a definite value, it returns 1. I am sure that I have not accidentally initialized it to true anywhere. Just curious, is it supposed to be this way?

          Cheers,
          Crispin
          since bools can take only two values
          by default it should be zero .due some mistake in the system hardware it might happen

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by vishwanathntand asi
            since bools can take only two values
            by default it should be zero .due some mistake in the system hardware it might happen
            That's not true, actually - there is nothing wrong with the OP's hardware.

            Check it for yourself - I just did (though for some reason I get an error when I try to copy and paste, so can't copy my code...) But if you initialize four values to: any arbitrarily large number, one that is large and negative, one to one, and one to zero, then put them all in if statements that print, the only one in gcc that will not print is the one with 0. This is a compiler quirk, not a hardware problem.

            Comment

            • willakawill
              Top Contributor
              • Oct 2006
              • 1646

              #7
              Originally posted by vishwanathntand asi
              since bools can take only two values
              by default it should be zero .due some mistake in the system hardware it might happen
              Nonsense. The compiler has to treat any non zero value as true or 1. This is called undefined behaviour because we don't know what will be at that memory address without initializing the variable, bool or otherwise.

              Comment

              Working...