bug-gnulib
[Top][All Lists]
Advanced

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

Re: [Bug-gnulib] Re: snprintf


From: Bruno Haible
Subject: Re: [Bug-gnulib] Re: snprintf
Date: Fri, 1 Oct 2004 21:30:07 +0200
User-agent: KMail/1.5

Paul Eggert wrote:
> > snprintf (char *str, size_t size, const char *format, ...)
> > {
> >   ...
> >   len = size;
> >   output = vasnprintf (str, &len, format, args);
>
> This assumes that STR is malloc-allocated, no?  I thought vasnprintf
> will reallocate STR if SIZE is too small.  But STR might point to
> static storage.

Good point. But actually vasnprintf will not reallocate STR, rather it will
allocate fresh memory, if SIZE is too small. The purpose is precisely so
that it can be used with a stack-allocated buffer:

   char buf[100];
   size_t len = sizeof (buf);
   char *output = vasnprintf (buf, &len, format, args);
   ...
   if (output != buf)
     free (output);

Bruno





reply via email to

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