Re: [dev] [dwm][st] why use ints over bools?

From: Mattias Andrée <maandree_AT_kth.se>
Date: Sat, 15 Jun 2024 14:36:16 +0200

I have some general issues with _Bool:

Arithmetic or bitwise operations on _Bool is subject to type promotion
making them rather pointless.

With also be a problem if they were not, as you than couldn't sum of _Bool's
to find how many were set (commonly this we be to detect if more than 1 was
set).

It is implementation-define which signness _Bool has, meaning, I don't know
if it will be promoted to a signed or an unsigned int.

_Bool is only useful to make sure that a non-zero value is converted to the
value 1. And if this is absolutely necessarily, you should make this explicit,
e.g. with `!!x` or `x?1:0`. _Bool can introduce implicit and unnecessary
overhead to ensure that the value is always 1 or 0. And despite all this,
you can mess up and get _Bool's with a non-boolean value.

It is implementation-defined how wide a _Bool is.

_Bool will be at least the size of a byte, if you really want it to be a bit
if you have many of them, you can use bit fields or good old names values to
store multiple boolean values in one int. Generally you don't gain any real
savings by using a single byte type instead of an int if you only one or two
booleans.

Basically, I like things to be explicit and well-defined.



On Sat, 15 Jun 2024 11:54:02 +0000
Zac <maczac_AT_protonmail.com> wrote:

> In a number of spots in dwm and st I've seen the use of integers as booleans. e.g.
> - dwm.c line 95, 140, 1870
> - drw.c line 252
> - st.c line 44, 48
>
> That's not an extensive list; just some examples.
>
> I'm curious why you use ints though. Because bools are 31 bits smaller than ints, which is 31 bits of memory saved. Not that 31 bits is very much, but still...
>
Received on Sat Jun 15 2024 - 14:36:16 CEST

This archive was generated by hypermail 2.3.0 : Sat Jun 15 2024 - 15:24:08 CEST