"Xiaobin Yang" <xy***@esat.kuleuven.ac.be> wrote in message
news:Pi******************************@speyburn.esa t.kuleuven.ac.be...
Why is the Class size 4-bytes instead of 1 byte as I intend to ?
=================================
class BitTest {
public:
unsigned eightBits : 8;
};
int main()
{
cout << "size of BitTest: " << sizeof(BitTest) << endl;
}
==================================
The language standard allows an implementation to
insert any amount of 'padding' after any struct/class member
(but not before the first). This is typically done in the
interest of 'efficiency' and/or to meet the host platform's
alignment requirements.
Some implementations (for platforms on which such things
are possible) provide a #pragma to control struct 'packing'.
Check your documentation.
But using such a construct renders the code nonportable.
You also seem to be exhibiting a misconception:
that 'byte' means 'eight bits'. The language requires
type 'char' (which is synonymous with 'byte') to have
a *minimum* of eight bits, but does not prohibit a
larger size (e.g. 9, 16, 32, etc.).
-Mike