emacs-devel
[Top][All Lists]
Advanced

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

Re: master 31544bc: Don’t convert pointer to bool


From: Matt Armstrong
Subject: Re: master 31544bc: Don’t convert pointer to bool
Date: Sat, 20 Mar 2021 11:21:03 -0700

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>     Without this patch, Oracle Studio 12.6 complains about converting
>>     pointer to bool.
>
> Is it right to complain or is that a bug in that tool?
> To my naive intuition, it looks wrong since conversion to bool is
> standard practice when doing `if (ptr) ...`.

In code portable to pre-C99 the diff looks right to me.

Commit 31544bc was about assignment to "bool", not "if (ptr)...", and
the differece is relevant.

"if (ptr)..." and the like has always been valid C.  Instead, the
statememnts test the scalar expression against the zero value, which has
always well defined for pointers.

Essentially, in conditional statements there is an implicit "!= 0"
there.  There is no implicit "!= 0" when converting pointers to integral
types.

In older compilers without _Bool, "bool" usually devolves to char.

  void* p = ...;
  char c = p;

The above code has undefined behavior, since a pointer doesn't fit in a
char.  In practice, most compilers will generate code that assigns the
low order byte of p to b.  This can do things like set b zero for
non-zero pointers and set b to values outside [0, 1].

So, either of these are better:

  char c = !!p;
  char c = p ? true : false;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]