Back in December 2008, Keith Marshall responded to a request in this mailing list for how to deal with the lack of asprintf in mingw with a bit of code that provides equivalent function.

I wanted to double-check before integrating this into a project (a unix package I am also hoping to use in win32) a) that this is considered a viable way to provide this functionality, and b) what license terms might apply. I know some portions of mingw are public domain, but it's important to me that I know everything that goes into this is well documented.

Disclaimer: I'm not actually very competent with C, I'm just trying to get infrastructure working. As far as I can tell it SEEMS to work, but I wanted to double-check with you guys here.

Here's what I found in the two posts to the mailing list (including a revision in a followup to the original response):

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

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 -1;

return vsprintf( *sptr, fmt, argv );

int asprintf( char **sptr, char *fmt, ... )
int retval;
va_list argv;
va_start( argv, fmt );
retval = vasprintf( sptr, fmt, argv );
va_end( argv );
return retval;