0

I've heard there is a version of sprintf(), possibly a GNU/gcc extension which either allocates its own buffer which I must free() or perhaps works using the stack like alloca().

Either method is fine for me. Can anyone tell me what function I was thinking of?

hippietrail
  • 15,848
  • 18
  • 99
  • 158

1 Answers1

5

You probably mean asprintf ?

From the man page:


Description

The functions asprintf() and vasprintf() are analogues of sprintf() and vsprintf(), except that they allocate a string large enough to hold the output including the terminating null byte, and return a pointer to it via the first parameter. This pointer should be passed to free(3) to release the allocated storage when it is no longer needed.


Note that asprintf is a GNU extension, which is also found in various BSD implementations, but it's not in standard C or POSIX.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 2
    It doesn't matter too much that this function is nonstandard, because it can be implemented as a trivial wrapper around `vsnprintf` (standard C99) or `open_memstream` and `vfprintf` (POSIX 2008 and C89, respectively). – R.. GitHub STOP HELPING ICE Apr 01 '11 at 12:22