bool

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yashwant pinge

    #1

    bool

    I have written a small main function .
    I have defined one bool variable and cout this variable , I found
    the value of this bool variable is 64.

    Since, I am using 32-bit addressing .

    My program is


    int main()
    {
    bool ab;
    cout<<ab<<endl;

    }

    output is 64.

    Why it is...?
    It should be 32.
    May be I am wrong.

    Yashwant

  • Rolf Magnus

    #2
    Re: bool

    yashwant pinge wrote:
    I have written a small main function .
    I have defined one bool variable and cout this variable , I found
    the value of this bool variable is 64.
    That would be very strange.
    Since, I am using 32-bit addressing .
    What does the value of a bool have to do with the size of an address?
    My program is
    >
    >
    int main()
    {
    bool ab;
    cout<<ab<<endl;
    >
    }
    cout and endl aren't defined. Also your bool is not initialized, so the
    result is undefined.
    output is 64.
    >
    Why it is...?
    It should be 32.
    May be I am wrong.
    I have no idea why you would think it should be 32. The only values your
    program should print, once you change your definition of ab to initialize
    it), are 1 (if ab's value is 'true') and 0 (if it's 'false').

    Comment

    • josh

      #3
      Re: bool

      On 14 Mar, 12:17, "yashwant pinge" <yashwantpi...@ gmail.comwrote:
      I have written a small main function .
      I have defined one bool variable and cout this variable , I found
      the value of this bool variable is 64.
      >
      Since, I am using 32-bit addressing .
      >
      My program is
      >
      int main()
      {
      bool ab;
      cout<<ab<<endl;
      >
      }
      >
      output is 64.
      >
      Why it is...?
      It should be 32.
      May be I am wrong.
      >
      Yashwant
      here as you dont't have assigned any explicit value to it than the
      value is random
      and don't confusing with the addressing in fact:

      bool ab;
      int cc;

      cout<< "not assigned value of ab= " << ab <<endl;
      cout<< "not assigned value of cc= " << cc <<endl;

      cout<< "sizeof ab= " << sizeof ab <<endl;
      cout<< "sizeof cc= " << sizeof cc <<endl;

      on my 32 bit machine output this:
      >>not assigned value of ab= 191
      >>not assigned value of cc= 8822772
      >>sizeof ab= 1
      >>sizeof cc= 4
      Joshua




      Comment

      • Jim Langston

        #4
        Re: bool

        "yashwant pinge" <yashwantpinge@ gmail.comwrote in message
        news:1173871048 .609532.108060@ y80g2000hsf.goo glegroups.com.. .
        >I have written a small main function .
        I have defined one bool variable and cout this variable , I found
        the value of this bool variable is 64.
        >
        Since, I am using 32-bit addressing .
        >
        My program is
        >
        >
        int main()
        {
        bool ab;
        cout<<ab<<endl;
        >
        }
        >
        output is 64.
        >
        Why it is...?
        It should be 32.
        May be I am wrong.
        >
        Yashwant
        your bool variable ab is not initialized. So it contains anything that was
        in the memory where it is allocated. In your case it happened to contain
        the value 32.

        Try this instead:

        bool ab = true;
        cout << ab << endl;

        it should output the value 1.

        If you want to know the size of the variable, use sizeof.

        bool ab = true;
        cout << sizeof ab << endl;


        Comment

        • yashwant pinge

          #5
          Re: bool

          On Mar 14, 5:08 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
          "yashwant pinge" <yashwantpi...@ gmail.comwrote in message
          >
          news:1173871048 .609532.108060@ y80g2000hsf.goo glegroups.com.. .
          >
          >
          >
          I have written a small main function .
          I have defined one bool variable and cout this variable , I found
          the value of this bool variable is 64.
          >
          Since, I am using 32-bit addressing .
          >
          My program is
          >
          int main()
          {
          bool ab;
          cout<<ab<<endl;
          >
          }
          >
          output is 64.
          >
          Why it is...?
          It should be 32.
          May be I am wrong.
          >
          Yashwant
          >
          your bool variable ab is not initialized. So it contains anything that was
          in the memory where it is allocated. In your case it happened to contain
          the value 32.
          >
          Try this instead:
          >
          bool ab = true;
          cout << ab << endl;
          >
          it should output the value 1.
          >
          If you want to know the size of the variable, use sizeof.
          >
          bool ab = true;
          cout << sizeof ab << endl;
          since the bool is internally converted as 1 and 0. So why the size of
          bool is one?

          Comment

          • =?ISO-8859-1?Q?Erik_Wikstr=F6m?=

            #6
            Re: bool

            On 2007-03-14 14:05, yashwant pinge wrote:
            On Mar 14, 5:08 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
            >"yashwant pinge" <yashwantpi...@ gmail.comwrote in message
            >>
            >news:117387104 8.609532.108060 @y80g2000hsf.go oglegroups.com. ..
            >>
            >>
            >>
            >I have written a small main function .
            I have defined one bool variable and cout this variable , I found
            the value of this bool variable is 64.
            >>
            Since, I am using 32-bit addressing .
            >>
            My program is
            >>
            int main()
            {
            bool ab;
            cout<<ab<<endl;
            >>
            }
            >>
            output is 64.
            >>
            Why it is...?
            It should be 32.
            May be I am wrong.
            >>
            Yashwant
            >>
            >your bool variable ab is not initialized. So it contains anything that was
            >in the memory where it is allocated. In your case it happened to contain
            >the value 32.
            >>
            >Try this instead:
            >>
            > bool ab = true;
            > cout << ab << endl;
            >>
            >it should output the value 1.
            >>
            >If you want to know the size of the variable, use sizeof.
            >>
            > bool ab = true;
            > cout << sizeof ab << endl;
            >
            since the bool is internally converted as 1 and 0. So why the size of
            bool is one?
            >
            Because 1 is the smalles possible value. Theoretically a bool does not
            need to take more space than on bit, but due to alignment requirements
            and such that would leave at least 7 bits in that byte unused, so the
            effective space used would still be at least one byte. Note though that
            the 1 of sizeof(bool) does not mean 1 byte, but rather a piece of memory
            as large as a char (which is the smallest addressable unit in C++),
            which just happens to be one byte on most machines.

            --
            Erik Wikström

            Comment

            • Gavin Deane

              #7
              Re: bool

              On 14 Mar, 13:53, Erik Wikström <Erik-wikst...@telia. comwrote:
              Note though that
              the 1 of sizeof(bool) does not mean 1 byte,
              Yeah it does. sizeof returns a number of bytes ...
              but rather a piece of memory
              as large as a char (which is the smallest addressable unit in C++),
              .... and a char is 1 byte
              which just happens to be one byte on most machines.
              Maybe there could be a difference between the size of a byte as
              understood by the C++ memory model (section 1.7 of the standard) and a
              byte as understood by the people who designed the target processor -
              then it would be the compiler's job to map between the two in a manner
              opaque to the programmer. But within the scope of the C++ standard,
              sizeof returns a number of bytes dy definition and a char is 1 byte by
              definition.

              Gavin Deane

              Comment

              • Ron Natalie

                #8
                Re: bool

                Erik Wikström wrote:
                >
                Because 1 is the smalles possible value. Theoretically a bool does not
                need to take more space than on bit, but due to alignment requirements
                and such that would leave at least 7 bits in that byte unused, so the
                effective space used would still be at least one byte. Note though that
                the 1 of sizeof(bool) does not mean 1 byte, but rather a piece of memory
                as large as a char (which is the smallest addressable unit in C++),
                which just happens to be one byte on most machines.
                >
                The above is wrong on several accounts.

                First, byte in C++ is defined in terms of char. A char
                variable by definition in the language is one byte.
                This unfortunately is a major defect in C++ portability.
                char plays double duty:

                1. It's the native character type.
                2. It's the smallest addressable storage unit.

                When all the world is eight bit ascii this is not an
                issue. It's problematic when the native character
                is LARGER than the minimal addressable storage
                (for example a machine with addressable units
                of 8 bits, but the native char wants to be 16).
                wchar_t is a poor substitute as there are a number
                of important interfaces that do not have wchar_t
                analogs. You have to kludge it by inventing
                a multibyte (conversion of the wchar_t type to
                a sequence of chars) even when such a thing
                doesn't uniquely exist.

                Further, no C++ type can be less than the byte
                size (sizeof char). This is also fundamanetal.
                It has nothing to do with alignment. It has to
                do with the fact that C++'s pointer behavior
                assumes that things aren't smaller than char.

                Comment

                Working...