On 2025-08-13 04:27:53 +0000, Dave Hart wrote: > On Thu, Aug 7, 2025 at 8:52 PM Paul Eggert <egg...@cs.ucla.edu> wrote: > > > That thing with malloc goes too far. 'char *p = malloc (...);' is a good > > idiom in C, and it's silly and even counterproductive to require a cast. > > I completely agree. Adding the cast requires duplicating the type, slows > comprehension, and invites copy/paste mischief.
One may disagree. This can be a protection in case of the type of p changes (the declaration and the use of malloc are not necessarily found at the same place). I'm tempted to use the following macro in my programs: #define MALLOC(N,T) (((T)*) malloc ((N) * sizeof (T))) (not tested, but this gives the idea). If used in an incorrect pointer type context, one would get a warning or an error. > > I worked around the glitch by not using malloc. > > I've seen this sort of change elsewhere. I'm guessing at least one > compiler is emitting a warning without the superfluous cast. C compilers that are actually C++ compilers may give this warning by default (I would tend to say Microsoft's compiler, then). There's the -Wc++-compat option with GCC. > Can someone enlighten me why some folks apparently believe an > implicit cast from void pointer to another pointer is evil? Not evil, but from the compiler's point of view, that's due to C++, which has stronger type checking than C (this is not limited to the use of malloc). See also above. -- Vincent Lefèvre <vinc...@vinc17.net> - Web: <https://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / Pascaline project (LIP, ENS-Lyon)