开发者

C sprintf function that uses malloc or the stack [duplicate]

开发者 https://www.devze.com 2023-02-21 22:35 出处:网络
This question already has answers here: sprintf() with automatic memory allocation? (8 answers) Closed 8 years ago.
This question already has answers here: sprintf() with automatic memory allocation? (8 answers) Closed 8 years ago.

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消