Asprintf - when to use free ()?

asprintf says

The functions asprintf () and vasprintf () are analogous to sprintf (3) and vsprintf (3), except that they select a string that is large enough to hold the output, including the terminating null byte, and returns a pointer to it through the first argument. This pointer should be handed over free of charge (3) to release the allocated storage when it is no longer needed.

Here is my C code

void function(){ char *out = NULL; int parts[16] = {1,2,05,003}; asprintf(&out, "%d.%d.%d.%d", parts[0], parts[1], parts[2], parts[3]); // free(out); } int main(void){ function(); return 0; } 

When a function is tracked in debug mode, I see that the variable is already destroyed when it returns from the function. Why don't I need the free() code above?

Could you tell me in what cases asprintf I need to use asprintf for free?

btw, I have "gcc version 4.7.2 (Ubuntu / Linaro 4.7.2-2ubuntu1)"

enter image description hereenter image description here

+4
source share
4 answers

You need to call free() .

The variable leaves scope, so the actual value of the variable (the address that free() needs) is lost at this point, creating a memory leak.

The free() function is interested in the memory address previously returned by malloc() or another heap allocation call, and not in your specific variable at all.

You can do:

 char *out = NULL, *a, *b, *c, *d, *e; int parts[16] = {1,2,05,003}; asprintf(&out, "%d.%d.%d.%d", parts[0], parts[1], parts[2], parts[3]); a = b = c = d = e = out; 

and that five more variables contain copies of the same address that go beyond. Of course, the heap subsystem (upon reaching through malloc() / free() calls) knows nothing about this.

+10
source

Could you tell me in what cases do I need to use asprintf for free?

Each time, unless asprintf returns -1.

If memory allocation was not possible or any other error occurred, these functions will return -1, and the contents of strp will be undefined .

You can, for example, analyze the output of valgrind.

  • Without calling free :

== 3711 == HEAP SUMMARY:
== 3711 == used on exit: 8 bytes in 1 block
== 3711 == total heap usage: 2 allocations, 1 frees, 108 bytes allocated

Obviously, a memory leak is occurring.

  • With free call:

== 3722 == HEAP SUMMARY:
== 3722 == used on exit: 0 bytes in 0 blocks
== 3722 == total heap usage: 2 allocations, 2 bits, 108 bytes allocated

+5
source

It seems you do not quite understand what pointers are, what local variables are and what memory is allocated.

In your function() you have an out variable. You indicated that it is a char pointer.

You pass a pointer to this variable on asprintf() . This means that asprintf can allocate some memory on the heap and store the address of this memory location in out . This is called a "pointer to a pointer" and why the asprintf() signature appears as asprintf(char **ret, .. ) - note double **

out now contains the address of the allocated memory. Then you leave function() . out ceases to exist because it is a local variable and exists only on the stack. The address that was previously contained in it was lost, but the memory that was allocated on the heap, the specified address remained allocated ... and you have a memory leak.

You need free() to save this memory before you lose the address, and it becomes permanently inaccessible.

+5
source

out "destroyed", but not what indicated. The latter is still distributed, but any link to it is lost. You may consider this a memory leak.

To avoid such a call to free(out) before leaving function() .

An alternative solution would be to return the out value from function , so the caller can still use the specified memory. On the other hand, the caller also takes responsibility for freeing this memory:

 char * function(void) { char * out = NULL; int parts[16] = {1, 2, 05, 003}; asprintf(&out, "%d.%d.%d.%d", parts[0], parts[1], parts[2], parts[3]); return out; } int main(void) { char * out = function(); if (NULL != out) { printf("5s\n", out); free(out); out = NULL; } return 0; } 
+4
source

Source: https://habr.com/ru/post/1488694/


All Articles