On Sunday 14 December 2008 19:38:40 Jonathan Schleifer wrote:
> > If you don't show us a minimal self-contained, (and complete),
> > test case, how can you expect us to see what you might be doing
> > wrong?
> The test case is the asprintf.c which I linked.
But, you didn't put it IN THE MAIL; there are few who will take the
trouble to chase web links, to assist you.
The point about a minimal test case is that it should be sufficient
to illustrate the problem, but small enough to post inline, within a
mail message; if you need to post a link, then you likely haven't
reduced the problem enough.
Note that asprintf() and vasprintf() are required by neither the ISO C
Standard, nor by POSIX; GNU and BSD provide them as extras. Having
said that, FWIW, here is how I might implement them:
$ cat <<EOF > asprintf.c
int asprintf( char **, char *, ... );
int vasprintf( char **, char *, va_list );
int vasprintf( char **sptr, char *fmt, va_list argv )
int wanted = vsnprintf( *sptr = NULL, 0, fmt, argv );
if( (wanted > 0) && ((*sptr = malloc( 1 + wanted )) != NULL) )
return vsprintf( *sptr, fmt, argv );
int asprintf( char **sptr, char *fmt, ... )
va_start( argv, fmt );
retval = vasprintf( sptr, fmt, argv );
va_end( argv );
int len = asprintf( &retbuf, "%s", "An allocated string." );
printf( "Allocated %d + 1 bytes for string '%s'\n", len, retbuf );
free( retbuf );
$ mingw32-gcc -o asprintf.exe asprintf.c
$ wine asprintf.exe
Allocated 20 + 1 bytes for string 'An allocated string.'