D
Davide Bruzzone
Greetings all...
I need to create a number of bitfield structs whose contents are
smaller than the size of an int. For example:
typedef struct foo FOO;
struct foo {
unsigned char fieldOne: 2, fieldTwo: 6;
};
I'd like this struct to take up no more than 8 bits, however, when I
call sizeof(FOO), the function returns 4 bytes (which is the size of
an int on the platform on which I'm compiling my code).
Now I know that according to the ANSI C standard, this is normal (i.e.
Bitfield structs are padded to the size of an int). What I'm having
trouble finding more/clearer information about is:
- Whether its possible to force the compiler to bend/break this rule,
and to create non-padded bitfield structs that take us, say, one byte
or two (BTW, I'm using GCC 2.95.1).
- If this is possible, how I go about doing it (i.e. What command-line
options I need to use, and what, if anything, I need to add to my
code).
Any suggestions, or pointers to the appropriate information would be
greatly appreciated.
Cheers...
Dave Bruzzone
I need to create a number of bitfield structs whose contents are
smaller than the size of an int. For example:
typedef struct foo FOO;
struct foo {
unsigned char fieldOne: 2, fieldTwo: 6;
};
I'd like this struct to take up no more than 8 bits, however, when I
call sizeof(FOO), the function returns 4 bytes (which is the size of
an int on the platform on which I'm compiling my code).
Now I know that according to the ANSI C standard, this is normal (i.e.
Bitfield structs are padded to the size of an int). What I'm having
trouble finding more/clearer information about is:
- Whether its possible to force the compiler to bend/break this rule,
and to create non-padded bitfield structs that take us, say, one byte
or two (BTW, I'm using GCC 2.95.1).
- If this is possible, how I go about doing it (i.e. What command-line
options I need to use, and what, if anything, I need to add to my
code).
Any suggestions, or pointers to the appropriate information would be
greatly appreciated.
Cheers...
Dave Bruzzone