bools and bit-fields

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,
There was some discussion on IRC in the past about this, but just to get
everybody's view on this ...

We currently define bools in various structures as:

    pa_bool_t my_bool:1;

While this seems innocuous, there are potentially subtle bugs of the
following kind:

    int x = TRUE;
    struct_with_bool y;
    y.my_bool = TRUE;

    if (x == y.my_bool)
        solve_world_peace();

The condition in the if statement may evaluate to false if pa_bool_t is
typedef'ed to a signed type (it definitely is if you don't have
stdbool.h, not sure if _Bool is a signed type or not). This happens
because it's a single bit field and thus when the msb (the only bit) is
set to 1 it is interpreted as -1.

Yes, this sort of comparison isn't common, but it can be quite
non-obvious and frustrating if you end up hitting a bug because of this.
The extra memory used will be in the range of a few thousand bytes in
the pathological case.

In my opinion, the space trade-off is worth it. Thoughts?

Cheers,
Arun



[Index of Archives]     [Linux Audio Users]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux