Questions tagged [asprintf]

Use this tag for questions about the C asprintf and vasprintf extension functions for memory-safe data formatting.

asprintf is a common C runtime function provided as an extension to the Standard C Library. It was originally provided by GLIBC, but has since made its way into a variety of vendor-provided C runtime libraries. 3rd-party implementations exist for virtually every platform.

asprintf has a variadic version called vasprintf, which correlates in use to the standard vsprintf function, taking the asprintf semantics for buffer and return value. Henceforth, both will simply be referred to as asprintf, except where there are differences worth noting.

On GLIBC environments, such as Linux, asprintf is available from stdio.h, like its sprintf counterpart, as such:

#include <stdio.h>

Other libraries, and in particular 3rd-party libraries, require different header files included to use asprintf.

The function signatures are:

int asprintf(char **strp, const char *fmt, ...);
int vasprintf(char **strp, const char *fmt, va_list ap);

The asprintf function may be used similarly to the C Standard sprintf, except that an allocated buffer is not provided to the function, but is allocated by it. The buffer is allocated dynamically, so must also be explicitly freed. The return value is the length of the allocated buffer, and the buffer is returned via strp. On failure, the return value is -1, and the contents of strp are undefined.

For usage, consider this example code snippet:

char *str = NULL;
int size = asprintf(&str, "this is a %s", "test");

printf("%s\n", str); // this is a test
printf("%d\n", size); // 14

free(str);

This will print the following:

this is a test
14

Additional Resources

23 questions
70
votes
2 answers

Why use asprintf() instead of sprintf()?

I'm having a hard time understanding why you would need asprintf. Here in the manual it says The functions asprintf() and vasprintf() are analogs of sprintf(3) and vsprintf(3), except that they allocate a string large enough to hold the output…
Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
13
votes
4 answers

Substitute or workaround for asprintf on AIX

I'm trying to build python-kerberos on AIX. kerberospw.c uses a call to asprintf, but from what Google is telling me, asprintf does not exist on AIX. I saw http://www.koders.com/c/fidAA9B130D588302673A28B568430A83131B7734C0.aspx?s=windows.h, which…
bobwood
  • 200
  • 3
  • 8
11
votes
8 answers

Using asprintf() on windows

I have written a C program which works perfectly on linux, but when I compile it on windows, it gives me an error saying that asprintf() is undefined. It should be a part of the stdio library but it seems that many compilers do not include it. Which…
Vasundhara Mehta
  • 278
  • 2
  • 3
  • 16
9
votes
1 answer

Best practice with sprintf?

Here's the situation: We received code from an outside source that uses sprintf like strcat. Like this: char buffer[1024]; sprintf(buffer, "Some text."); sprintf(buffer, "%s%s", buffer, "Some more text"); sprintf(buffer, "%s%s", buffer, "again more…
6
votes
2 answers

Is `asprintf` thread-safe?

Is the GNU function asprintf (print to allocated string) thread-safe? (IIC, basically, this boils down to the question whether malloc is thread-safe.) Consider the example code: #define _GNU_SOURCE #include #include "getValue.h" char *…
imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104
3
votes
1 answer

Displaying time string created using asprintf

I want display a string in this form: "in 3 days 00:15:07" or "in 00:15:07" in the case days is 0 so I wrote some code as follows #include #define LEASE_TIME_USING_DAYS "in %d days %c%d:%c%d:%c%d\n" #define LEASE_TIME_NOTUSING_DAYS "in…
Kallel Omar
  • 1,208
  • 2
  • 17
  • 51
2
votes
1 answer

Check if `_GNU_SOURCE` or `_BSD_SOURCE` is supported by the libc?

I am curious if asprintf is available. Some libc implementations provide it under specific Feature Test Macros. Namely if you #define _GNU_SOURCE or #define _BSD_SOURCE you can get asprintf and a few other nice nonstandard extensions when you…
Samuel Marks
  • 1,611
  • 1
  • 20
  • 25
1
vote
1 answer

Dynamically allocate `char*` with concatention and format literals?

With support for macOS, Windows (MSVC), and Linux, how do I do the following? char *s; func(&s, "foo"); if () func(&s, "bar%s", "can") /* want "foobarcan", and I don't know `strlen(s)` AoT */ I've tried with asprintf (was able to find an…
A T
  • 13,008
  • 21
  • 97
  • 158
1
vote
1 answer

How do I convert a long long into a string?

I am creating a program that takes a numerical input from the user as a long long, and then there is some maths involved with individual digits. I think the best way to do this is to convert the long long to a string and then iterate over the…
EdCase
  • 119
  • 2
  • 7
1
vote
1 answer

Segmentation fault in a long string

I am writing a function which prints out to as standard output, like how a regular printf function does but instade of taking indicators like %d or %s it takes {i} or {s}. The problem i have is that when the string of the format argument is too…
F.MANNOU
  • 23
  • 1
  • 7
1
vote
4 answers

php sprintf HTML template

I have html mail template which I want to send to my users by php mail() function. I'm putting this template to php variable and want to insert into that the specified variables using sprintf() function but I'm getting this- Warning: sprintf():…
David
  • 111
  • 2
  • 13
1
vote
1 answer

Invalid read of size 1 when using valgrind

I am getting this output when using valgrind: ==19923== Invalid read of size 1 ==19923== at 0x52CCCC0: vfprintf (vfprintf.c:1632) ==19923== by 0x52F4772: vasprintf (vasprintf.c:59) ==19923== by 0x52D3A56: asprintf (asprintf.c:35) ==19923== …
MuffinMan1042
  • 75
  • 1
  • 11
1
vote
2 answers

Empty file when using sprintf and system function on C

I want to save some information in a file text, I wrote this program: #include #include #include int main(int argc,char *argv[]) { FILE *fichier; char buffer[20]; char command[200]; …
Slim Hmidi
  • 25
  • 3
0
votes
2 answers

asprint memory leak need help understand where leak is coming from and possible fixes

Note: I did call this function and free it main but valgrind still shows error. This code basically takes in a singly linked-list with two data coeff and exp. This is basically converting a polynomial store in a linked list converted to readable…
0
votes
1 answer

R - For Loop through Rows of Dataframe + Write Long Text to File

I am struggling with a particular for loop in R. I have a data frame with 52 rows and about 30 columns. I am writing the for loop to extract data (or values) from each row and to insert them into a long text --- in this case a javascript/geojson…
Vee_R
  • 1
  • 1
1
2