[Top][All Lists]

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

Re: [GNUnet-developers] [patch] add GNUNET_append_printf function

From: Christian Grothoff
Subject: Re: [GNUnet-developers] [patch] add GNUNET_append_printf function
Date: Fri, 14 Feb 2014 14:59:16 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10

realloc is not nice for various reasons:

1) realloc can fail; ever read what happens to the memory in that case?
   Seen code that handles this cleanly?
2) arguments that are IN/OUT are a bit ugly in general;
3) side-effects on pointers can cause problems with aliasing
   (i.e.  x = y; z = realloc (x); y dangles)

Also, it is generally much better for performance to first calculate
precisely how much memory you need, and then alloc and use it.  Even
if you need to iterate over the data twice.  That's what
GNUNET_asprintf does, and encouraging developers to do the slow
'realloc-to-append' operation is not really something we should

Now, of course there is no rule without an exception, so I'm not saying
that we must never 'realloc', just that if in doubt, the answer is
probably to avoid it.  See 'goto' -- we use it, but with care.

My 2 cents

P.S.: some selected opinions on the matter:

On 02/14/2014 02:50 PM, Andrew Cann wrote:
> It just seems cleaner to me to start with a buffer and realloc it a few times
> until it's the size I want. Rather than malloc() and free() a bunch of
> intermediate buffers of incrementally greater sizes. It involves less copies
> aswell so long as the buffer can be realloc()ed in place.
> Why do you say realloc is bad style?
>  - Andrew
> On Fri, Feb 14, 2014 at 02:30:04PM +0100, Christian Grothoff wrote:
>> Why not just use
>> GNUNET_asprintf (&result, "%s" format, prefix, ...)?
>> The only difference to your new API is that you imply a realloc and
>> thus would not have to 'free' prefix, but I think that's still cleaner
>> (I personally try to avoid realloc, it is kind-of bad style anyway).
>> Just wondering...
>> -Christian
>> On 02/14/2014 01:01 PM, Andrew Cann wrote:
>>> This patch adds a function which realloc()s a string and prints to the end 
>>> of
>>> it. Useful for building up strings when you don't know how large a buffer to
>>> allocate beforehand.
>>> I've also added the format(printf) __attribute__ to this and the other 
>>> *printf
>>> functions.
>>>  - Andrew
>>> _______________________________________________
>>> GNUnet-developers mailing list
>>> address@hidden
>> pub  1024D/48426C7E 2006-12-18 Christian Grothoff <address@hidden>
>> uid                            Christian Grothoff <address@hidden>
>> uid                            Christian Grothoff <address@hidden>
>> uid                            Christian Grothoff <address@hidden>
>> uid                            Christian Grothoff <address@hidden>
>> sub  4096g/6433E483 2006-12-18

Attachment: 0x48426C7E.asc
Description: application/pgp-keys

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

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