On 7/16/2011 3:33 AM, MikeP wrote:
First, is size of bool unspecified? Or just implementation
defined? The size of all types is implementation defined (with
a minimum size).
And I don't see the issue with regards to structs.
IIRC, many compilers default to making them the same size as char, or a
single byte. this tends to be most convinient as, bigger, and they are
just wasting space, and smaller, as messing with bits is awkward and
costly (as well as compromising their direct addressability, ...).
A bool is required to be addressable; sizeof(bool) is also
required to return an integral value. So a bool can't be
smaller than a char. On a byte addressable machine, there's
also no reason to make them bigger, and I would expect them to
usually be the same as a char. On a word addressable machine,
accessing single bytes may be more expensive than accessing a
complete word, and there are arguments both ways.
as for passing smaller integer types, these tend to be promoted
internally to the smallest convinient size (namely, int) anyways, and on
common architectures this doesn't add any real cost (both x86 and ARM
have instructions to load a byte from memory with sign or zero extension).
Depending on the context: on many machines, it may be just as
fast to simply pass an int, with garbage in all but the low/high
order byte (depending on endianness). The garbage doesn't
matter, of course, because the callee won't ever look at it.
granted, yes, this does in-fact still depend on the compiler
though.
As does just about everything else.