Original Post
This has been bugging me since last night. It is really for abstract, educational purpose so I'm not after a "bit-fields are evil, do not use them" response.
As stated the question I have concerns bit-fields; as in:
[source lang="cpp"]/*
** flags used to describe the state of a log file
*/
struct flags
{
unsigned open : 1;
unsigned redirected : 1;
unsigned error : 1;
unsigned : 5;
};[/source]Now, logically (based on the explanations of bit-fields) the statment: [source lang="cpp"]sizeof (struct flags) == 1[/source] Should be true.
However, (using Visual Studio 2010) the size of the structure is actually 4 'bytes'. Further reading statement that bit-fields are highly implementation dependent in C. So I put the size down to that and moved on.
But now, grammar states that the 'sizeof' operator cannot be applied to bit-fields.
So this leaves me with a question: Is the operator actually returning the right value and bit-fields aren't actually implemented as the specification states, or is it the fact that the operator is wrong and I can take it on blind faith that the structure is the right 'wide'.
Other experiments (wider members) produce larger values for the size of the structure.
As stated the question I have concerns bit-fields; as in:
[source lang="cpp"]/*
** flags used to describe the state of a log file
*/
struct flags
{
unsigned open : 1;
unsigned redirected : 1;
unsigned error : 1;
unsigned : 5;
};[/source]Now, logically (based on the explanations of bit-fields) the statment: [source lang="cpp"]sizeof (struct flags) == 1[/source] Should be true.
However, (using Visual Studio 2010) the size of the structure is actually 4 'bytes'. Further reading statement that bit-fields are highly implementation dependent in C. So I put the size down to that and moved on.
But now, grammar states that the 'sizeof' operator cannot be applied to bit-fields.
So this leaves me with a question: Is the operator actually returning the right value and bit-fields aren't actually implemented as the specification states, or is it the fact that the operator is wrong and I can take it on blind faith that the structure is the right 'wide'.
Other experiments (wider members) produce larger values for the size of the structure.