bug-gnulib
[Top][All Lists]
Advanced

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

Re: Type-safe typecasts


From: Marc Nieper-Wißkirchen
Subject: Re: Type-safe typecasts
Date: Tue, 6 Apr 2021 21:13:58 +0200

Hi Paul,

thanks!

By the way, the snippet you gave is not portable C code, as it assumes
that 'void *' and 'struct foo *' have the same machine representation.
This is not necessarily true on (admittedly now-rare) machines that have
different flavors of pointers. I suspect the main problem here is either
in the calling code, or in the API for gl_list_iterator_next: if it
returned a possibly-null value of type 'const void *' instead of storing
the result through a 'const void **' pointer, the calling code wouldn't
have gotten into this portability mess.

gl_list_iterator_next has to return two things: An element (represented by a const void *) and a boolean value. As elements may be NULL, the boolean value is really needed. Of course, one could switch the actual return parameter with the one returned through a pointer but then it wouldn't look as nice in while or for-loops.

So to make my original code portable C, I would have to code

...
const void *e;
while (gl_list_iterator_next (&i, &e, NULL))
  {
    struct foo *foo = (void *) e;
    ++foo->bar;
  }
...

The const typecast is, unfortunately, still needed to silence compiler warnings as the Gnulib list API suffers a bit from const-poisoning when the actual elements are pointers actually non-const objects.

Marc

reply via email to

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