bug-gnulib
[Top][All Lists]
Advanced

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

Re: [bug-gnulib] Re: extend the scope of xasprintf


From: Bruno Haible
Subject: Re: [bug-gnulib] Re: extend the scope of xasprintf
Date: Wed, 10 May 2006 14:16:57 +0200
User-agent: KMail/1.5

Mark D. Baushke wrote:
> > static inline char *
> > xstrcat (size_t argcount, va_list args)
> > {
> >   char *result;
>
> ...elided...
>
> >   /* Allocate and fill the result string.  */
> >   result = (char *) xmalloc (totalsize + 1);
>
> Why is the cast needed? I was under the impression that GNULIB was using
> C89 semantics which should mean that
>
>   result = xmalloc (totalsize + 1);
>
> is all that is needed because 'void *xmalloc (size_t s);' should be the
> prototype and a 'void *' function should not need a cast.

The cast is not needed if
  1. you write ANSI C or ISO C code, and
  2. you are willing to rethink the code in depth each time you modify it.

I use the cast because

  1. I try to write code that is both valid ANSI C and C++ (this helps a lot
     in the Cygwin and mingw ports, due to the brokenness of shared libraries
     on that platform).

  2. When I manipulate code, I often use "correctness preserving 
transformations".
     For example:

        <type> result = <initializer>;
        return <expression>(result);

     where <expression> does not refer to result, can be transformed into

        return <expression>(<initializer>);

     Not so any more if you omit the casts:

        char *result = xmalloc (totalsize + 1);
        return result + 1;

     is valid C code, but

        return xmalloc (totalsize + 1) + 1;

     is not.

     The less you have to think when manipulating some code, the better.

Bruno





reply via email to

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