Re: [dev] [dwmstatus] smprintf rewritten (try)

From: <p37sitdu_AT_lavabit.com>
Date: Mon, 24 Dec 2012 01:21:09 +0400

On Sun, Dec 23, 2012 at 09:51:27PM +0100, Thuban wrote:
> Hello,
> I tried to rewrite smprintf to use asprintf, following your advices.
> Can you check if there is anything wrong?
> I have some doubt about the necessaries free(). Is it alright if free()
> is use outside the function?
>
> The code :
>
>
> char *
> smprintf(char *fmt, ...)
> {
> va_list fmtargs;
> char *buf = NULL;
>
> va_start(fmtargs, fmt);
> if (vasprintf(&buf, fmt, fmtargs) == -1){
> fprintf(stderr, "malloc vasprintf\n");
> exit(1);
> }
> va_end(fmtargs);
>
> return buf;
> }

It is alright to use free() outside the function, as long as it is
documented somewhere (in the comment before function, for example)
that memory should be freed by caller. For such short code it is ok
even without comments.
Received on Sun Dec 23 2012 - 22:21:09 CET

This archive was generated by hypermail 2.3.0 : Sun Dec 23 2012 - 22:24:04 CET