An interesting structure!
Shane Hathaway
shane at hathawaymix.org
Mon Nov 28 00:07:18 MST 2005
Laurent R wrote:
> Hello!
> I found an interesting thing about the structure. It is defined just like:
> struct
> {
> U16 A;
> bool B;
> U32 C
> }STR;
>
> for the application software, it may be coded with c and c++. the
> compiler is gcc version 3.3.2
> Now, the interesting thing arise, for c++ program, the value of
> sizeof(STR) is 8, but for c program, it is 12.
Normally, the size is not important. The compiler chooses a size for
types like "int" and "bool" that balances speed and memory consumption
on the specific target platform. Today's processors are designed in
such a way that manipulating a single bit is often more work than
manipulating a 32 bit integer, so a bool usually consumes the same space
as an int (I think.)
The size is important, however, when you need to read or write the
structure in binary format for storage or communication with other
processes. If you really need to control the size of the fields in your
structure, you should fall back to types that consume a specific number
of bits. You probably want "char B" instead of "bool B".
Shane
More information about the PLUG
mailing list