Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shruthi82
    New Member
    • Feb 2008
    • 1

    Query

    struct abc
    {
    int a: 4;
    char b: 2;
    int ab:5;
    };
    main()
    {
    struct abc w;
    cout << sizeof(w);
    }
    Question: The value of "w" is printed as 8 in Visual studio C++. How is the space allowed for int and char?
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by shruthi82
    struct abc
    {
    int a: 4;
    char b: 2;
    int ab:5;
    };
    main()
    {
    struct abc w;
    cout << sizeof(w);
    }
    Question: The value of "w" is printed as 8 in Visual studio C++. How is the space allowed for int and char?
    For integer, size is 4, and for char size is 1.

    Comment

    • Arulmurugan
      New Member
      • Jan 2008
      • 90

      #3
      Originally posted by shruthi82
      struct abc
      {
      int a: 4;
      char b: 2;
      int ab:5;
      };
      main()
      {
      struct abc w;
      cout << sizeof(w);
      }
      Question: The value of "w" is printed as 8 in Visual studio C++. How is the space allowed for int and char?

      Hi,
      Bit fields are completely depends on compiler, but from you example maximum 4 bytes are enough, and the same i can get from gcc also. i don't know why VC++ returns 8. check you compiler manual.

      Regards,
      Arul

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Bit fields are deprecated in C++.

        C++ developers use a bitset.

        Comment

        Working...