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

Re: auto-apt-0.3.22 hurd patch (PATH_MAX)



Hi!

I'll use the "static" buffer inside the function. But then I'll have
to make sure to free() the buffer after the last call to the function,
is that correct?
Just have to figure out where is the end of the code I guess!? ^_^'

Cheers,
Tanguy

2012/1/26 Jérémie Koenig <jk@jk.fr.eu.org>:
> On Thu, Jan 26, 2012 at 1:21 PM, Tanguy LE CARROUR
> <tanguy.lecarrour@gmail.com> wrote:
>> 2) The problem in pkg_symbol() is a bit different. The dynamically allocated
>> buffer is the return value of the function, so I cannot free it in the
>> function. The rest of the code that uses this buffer never frees it!
>> Am I supposed to fix each part of the code that uses this function to free
>> the returned buffer?!
>
> Presumably the returned buffer is static.
>
> You could use a static pointer to a buffer which is reallocated on
> each call; from the exterior the semantics would be close to
> identical:
>
>    char *fun(void)
>    {
>        static char *buf = NULL;
>        ...
>        buf = realloc(buf, whatever size is required this time);
>        ...
>        return buf;
>    }
>
> You'll be fine as long as the calling code does not assume the
> function returns a constant pointer.
>
> --
> Jérémie Koenig <jk@jk.fr.eu.org>
> http://jk.fr.eu.org/


Reply to: