[Nagiosplug-devel] usage of the bool type

Holger Weiss holger at CIS.FU-Berlin.DE
Tue Jun 5 20:30:11 CEST 2007


* Andreas Ericsson <ae at op5.se> [2007-06-05 11:13]:
> Matthias Eble wrote:
> > I was just wondering, why the bool type is never used in the plugin 
> > code. The only occurrence is in changes I made (two flags in 
> > check_disk). I've already heard, that int is actually faster to process 
> > despite the larger size, and that stdbool.h is not available on all 
> > platforms is that the reason?
>
> int is always the fastest to process because it's defined to be the same
> size as the registers on the CPU.

Neither is "int" defined that way nor does this assumption necessarily
hold in practice.  More importantly, "bool" is not defined to be of any
specific size (as long as it can hold the values "0" and "1"), so
compilers are free to use the size they deem most efficient or to
provide switches to let the user decide on the size/performance
trade-off.  In practice, sizeof(bool) equals 4 or even 8 on quite a few
platforms[*] and some compilers actually do provide such a switch.  If
all else fails, you can always "#define bool int", but as this is an
implementation-specific compiler issue, it's IMO nothing an application
programmer should care about.

> bool is a C++-ism that got pulled into C with C99.

So?

> A C-program gains nothing,

It could be argued that the program gains readability.

> but loses portability by using bool instead of int.

Not at all (if done correctly).

Don't get me wrong, I don't really care whether or not we use "bool".  I
just think the decision whether or not to use it should neither be based
on implementation-specific performance issues nor on the non-issue
"portability"; IMO it should be based purely on syntactic preference.

Holger

[*] IIRC sizeof(bool) actually was 8 while sizeof(int) was 4 at least
    with some GCC version on Alpha, precisely for this reason.

-- 
PGP fingerprint:  F1F0 9071 8084 A426 DD59  9839 59D3 F3A1 B8B5 D3DE




More information about the Devel mailing list