1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Sruct size

Discussion in 'C' started by ilans11il, Apr 17, 2013.

  1. ilans11il

    ilans11il New Member

    Joined:
    Apr 17, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi ,
    Could you please explane why the size of the struct is 4 byte?

    Code:
    struct bitfield
    {
      int a:5;
      int c:5;
      int b:6;
     
    };
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    Your machine likely uses 32 bits for ints - to verify, you can multiply the macro CHAR_BIT by sizeof(int). Since the bitfield you've posted is comprised of 16 bits, it needs the space of 1 int or 4 bytes on your machine. You can test it by adding more

    Code:
    struct bitfield
    {
        int a:5;
        int c:5;
        int b:6;
        int d:16;
        int e:32;
        int f:4;
    };
    a-d utilizes 32 bits, e utilizes 32 bits, and f utilizes 4 bits; the sizeof(bitfield) *should* require 12 bytes as space of 3 ints are needed.
     

Share This Page