McBob
McBob

Reputation: 1011

Sizeof Struct vs Sizeof Type

Im not "that" new to C but can some one please enlighten me on this one:

printf( "%d %d\n", sizeof( int ), sizeof( unsigned char ) );

print as expected 4 and 1.

typedef struct
{
    int a;
    unsigned char b;
} test

printf( "%d\n", sizeof( test ) );

print 8... Im really confused!

Upvotes: 0

Views: 232

Answers (1)

littleadv
littleadv

Reputation: 20272

Its called "alignment". Your struct is padded. You can "pack" it (different compilers have different ways of defining which type should be packed), and then it won't be aligned, but you might have run-time data access issues.

Upvotes: 1

Related Questions