sizeof a struct

Discussion in 'C Programming' started by Phui Hock, Nov 6, 2003.

  1. Phui Hock

    Phui Hock Guest

    Hi,
    I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
    bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
    hope someone can can explain to me. I can't find answer anywhere in my
    references nor on the net. Thank you very much.

    #include <stdint.h>

    typedef struct list{
    uint8_t a :6;
    uint8_t b :1;
    uint8_t c :1;
    struct {
    uint8_t x :4;
    uint8_t y :1;
    uint8_t z :3;
    } inner;
    /* struct list *next; */
    } test;

    int main(){
    printf("size of test is %d\n", sizeof(test));
    return 0;
    }
     
    Phui Hock, Nov 6, 2003
    #1
    1. Advertising

  2. Phui Hock

    Ben Pfaff Guest

    (Phui Hock) writes:

    > I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
    > bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
    > hope someone can can explain to me. I can't find answer anywhere in my
    > references nor on the net. Thank you very much.
    >
    > #include <stdint.h>
    >
    > typedef struct list{
    > uint8_t a :6;
    > uint8_t b :1;
    > uint8_t c :1;
    > struct {
    > uint8_t x :4;
    > uint8_t y :1;
    > uint8_t z :3;
    > } inner;
    > /* struct list *next; */
    > } test;


    The compiler is padding the 16 requested bits of bit-fields to 4
    bytes, probably for performance reasons. Compilers are allowed
    to add padding after structure members.
    --
    "IMO, Perl is an excellent language to break your teeth on"
    --Micah Cowan
     
    Ben Pfaff, Nov 6, 2003
    #2
    1. Advertising

  3. FAQ: sizeof a struct

    In article <>, Phui Hock wrote:
    > Hi,
    > I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
    > bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
    > hope someone can can explain to me. I can't find answer anywhere in my
    > references nor on the net. Thank you very much.


    Read this FAQ:

    http://www.eskimo.com/~scs/C-faq/q2.13.html

    --
    Andreas Kähäri
     
    Andreas Kahari, Nov 6, 2003
    #3
  4. Phui Hock

    Alex Guest

    Phui Hock <> wrote:
    > Hi,
    > I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
    > bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
    > hope someone can can explain to me. I can't find answer anywhere in my
    > references nor on the net. Thank you very much.


    > #include <stdint.h>


    > typedef struct list{
    > uint8_t a :6;
    > uint8_t b :1;
    > uint8_t c :1;
    > struct {
    > uint8_t x :4;
    > uint8_t y :1;
    > uint8_t z :3;
    > } inner;
    > /* struct list *next; */
    > } test;


    First of all, you are relying on the pointer to be of certain size.
    The C standard does not define how big such a pointer should be,
    it is up to your implementation.

    Second of all, your compiler is free to insert "padding" where ever
    it sees fit within the structure[1] in order to facilitate alignment
    that is more natural for your architecture or for other murkier
    reasons.

    [1]: There can never be padding before the first element.

    Alex
     
    Alex, Nov 6, 2003
    #4
  5. Phui Hock wrote:
    > Hi,
    > I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
    > bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
    > hope someone can can explain to me.


    Sure. A pointer is a variable, so it has a size in bytes. What did you
    expect ?

    I would say that pointers are probably 4 bytes long on your platform,
    but they also may be shorter and your compiler has added extra padding bits.

    Take care, the size of struct may not equal the total of it's members
    sizes.

    > I can't find answer anywhere in my
    > references nor on the net. Thank you very much.


    > #include <stdint.h>


    > typedef struct list{
    > uint8_t a :6;
    > uint8_t b :1;
    > uint8_t c :1;
    > struct {
    > uint8_t x :4;
    > uint8_t y :1;
    > uint8_t z :3;
    > } inner;
    > /* struct list *next; */
    > } test;
    >
    > int main(){


    it's int main(void) or int main(int argc, char **argv)

    > printf("size of test is %d\n", sizeof(test));


    call of printf() without definition
    (hint : you need to #include <stdio.h>)

    > return 0;
    > }



    [laotseu@localhost dev]$ gcc -Wall -ansi -pedantic -obytes bytes.c
    bytes.c:9: warning: bit-field `x' type invalid in ISO C
    bytes.c:10: warning: bit-field `y' type invalid in ISO C
    bytes.c:11: warning: bit-field `z' type invalid in ISO C
    bytes.c:5: warning: bit-field `a' type invalid in ISO C
    bytes.c:6: warning: bit-field `b' type invalid in ISO C
    bytes.c:7: warning: bit-field `c' type invalid in ISO C

    Woops. Non portable code ?

    With line 13 commented out :
    [laotseu@localhost dev]$ ./bytes
    size of test is 2

    With line 13 uncommented :
    [laotseu@localhost dev]$ ./bytes
    size of test is 8

    Did I say something about padding ?-)
    (NB : pointers are 4 bytes long on my platform.)

    Bruno
     
    Bruno Desthuilliers, Nov 6, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,371
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    651
    CBFalconer
    Apr 10, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,405
    Chris Fogelklou
    Apr 20, 2004
  4. Vinu
    Replies:
    13
    Views:
    1,462
    Lawrence Kirby
    May 12, 2005
  5. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    567
    Joe Smith
    May 22, 2006
Loading...

Share This Page