size of a structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahiapkum
    New Member
    • Mar 2007
    • 35

    size of a structure

    hello,
    I have a structure when i print the size of struture i get different values printed can anyone explain:

    struct m{
    unsigned int i;
    unsigned long int j;
    unsigned int l;
    unsigned int k:2;
    unsigned int m:2;
    };
    size is:24
    what about bit fields????
    struct m{
    unsigned int i;
    unsigned long int j;
    unsigned int l;
    unsigned int k:2;
    unsigned int m:2;
    }__attribute__( (__packed__));
    size is:17
    again what about bit fields???

    the size of int on my machine is 32bits..
  • arunraj2002in
    New Member
    • Jun 2007
    • 22

    #2
    Byte is the lowest level at which we can access the data, there is no bit type.
    In fact we can't perform operation on single bit(only by some tricks we can do).. every bit wise operation will be aplied on a entire byte at a time..
    So even if u apply bit field it will be applied to byte.. now if you use pack the also it will take minimum of byte only.

    --Arun--

    Comment

    • mahiapkum
      New Member
      • Mar 2007
      • 35

      #3
      Originally posted by arunraj2002in
      Byte is the lowest level at which we can access the data, there is no bit type.
      In fact we can't perform operation on single bit(only by some tricks we can do).. every bit wise operation will be aplied on a entire byte at a time..
      So even if u apply bit field it will be applied to byte.. now if you use pack the also it will take minimum of byte only.

      --Arun--
      My doubt is when i dont use packed the size of struct is 24 i.e size of long int ,is it that the size of other members of struct is based on the largest size member of struct.(here unsigned long int).But when i use packed the size is taken as normal size of members.
      When i dont use packed as in the first case the size is 24 i.e size of unsigned long int,which is also taken as size for two unsigned ints, and what about the two bit fields of 2 bits each.If size of structure is based on largest member of struct wouldnt it be 32.

      Comment

      • kky2k
        New Member
        • May 2007
        • 34

        #4
        Originally posted by mahiapkum
        My doubt is when i dont use packed the size of struct is 24 i.e size of long int ,is it that the size of other members of struct is based on the largest size member of struct.(here unsigned long int).But when i use packed the size is taken as normal size of members.
        When i dont use packed as in the first case the size is 24 i.e size of unsigned long int,which is also taken as size for two unsigned ints, and what about the two bit fields of 2 bits each.If size of structure is based on largest member of struct wouldnt it be 32.
        Though i can't understand ur question i can feel ur doubt...If i am right..
        then Its about padding in structures
        Let me explain that with a simple example,

        struct m{
        int a;
        double b;
        };

        if u guess,the size of the above structure is 12 then u r wrong 'Coz its 16
        y?
        go thru this link

        http://www.thescripts. com/forum/thread543879.ht ml

        Comment

        • mahiapkum
          New Member
          • Mar 2007
          • 35

          #5
          Originally posted by kky2k
          Though i can't understand ur question i can feel ur doubt...If i am right..
          then Its about padding in structures
          Let me explain that with a simple example,

          struct m{
          int a;
          double b;
          };

          if u guess,the size of the above structure is 12 then u r wrong 'Coz its 16
          y?
          go thru this link

          http://www.thescripts. com/forum/thread543879.ht ml
          Thanks for ur advice i understood.

          Comment

          Working...