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
0
votes
1 answer

How do I prevent asprintf writing over variables on heap?

I'm using asprintf to dynamically allocate memory and load strings to store information about files in working directory. In the 273rd (exactly and consistently) call of function parse_entry this line gets executed:file->filename_len =…
balast
  • 33
  • 5
0
votes
1 answer

Valgrind about asprintf: address is 0 bytes inside a block of size alloc'd

I have some code like this: void logConnectionStatus(char * domain, char * status, mqd_t logQueue) { char * message; asprintf(&message, "Connection to domain %s: %s", domain, status); mq_send(logQueue, message, 1024, 0); …
Kamo Spertsian
  • 785
  • 2
  • 8
  • 23
0
votes
0 answers

Valgrind reports freed bytes but unfreed blocks after asprintf()

I'm aware that asprintf() allocates memory and this needs to be freed after it's called. I added free statements for the pointers provided to asprintf (after they're used), yet valgrind reports that I still have memory leaks: ==2697== HEAP…
0
votes
4 answers

Converting int to char* in C when sprintf is too costly to use

I need to convert an int value to a char string, I used the following function where score is of type int. void updateScore(){ char str[5] = "0"; sprintf(str, "%d", score); drawString(str); } void drawString5x7(char *string){ while…
Omar Khalik
  • 13
  • 1
  • 1
  • 9
-1
votes
1 answer

Issue writing and reading from shared memory space c

The goal of this code is to create a shared memory space and write n's value to it in the child then print all the numbers generated in from the parent process. But this presently just prints out memory addresses like 16481443B4 which change every…
-2
votes
1 answer

C : why local variable is modifying with sprintf?

consider this example. I created a local scoped buffer below which can store 2 characters and one terminator. But when I cross that thresold with 2 digits number i.e a = 20 then something weird happens when I call sprintf. a gets modified and prints…
-2
votes
2 answers

asprintf function in C, what does it do?

I'm currently doing the Exploit-Exercises on level02 where in script I see a function called 'asprintf',asprintf(&buffer, "/bin/echo %s is cool" , getenv("USER")); buffer = null; before this function call. Please explain to me in plain english what…
DELETE_ME
  • 11
  • 1
  • 3
-2
votes
2 answers

asprintf - how to get string input in C

I am reading the book "21 century C" (first editon) and find a interesting program using asprintf to get the string without using malloc /size of for string length or space allocation. Please read the attached image from the same book to…
Chandra
  • 37
  • 2
  • 9
1
2