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.