Question: [asprintf](http://linux.die.net/man/3/asprintf) 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 including the terminating null byte, and return a pointer
>  to it via the first argument. **This pointer should be passed to free(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 the function is monitored on the debug mode, I see that the variable is already destroyed when it returns from the function. Why I don't need to `free()` the code above? 

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

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

![enter image description here](https://i.stack.imgur.com/jkb2n.png)
![enter image description here](https://i.stack.imgur.com/Z2q5E.png)

Answer: 
It seems you don't quite understand what pointers are, what local variables are, and what allocating memory does.

In your `function()` you have a variable `out`. You've declared that it is a char pointer.

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

`out` now contains the address of the allocated memory. You then leave `function()`. `out` ceases to exist because it's a local variable and only exists on the stack. The *address* that was previously contained in it is lost but the memory that was allocated on the heap that address referred to remains allocated ... and you have a memory leak.

You need to `free()` that memory before you lose the address and it becomes forever beyond your reach.
`out` is "destroyed", but not what is was pointing to. The latter is still allocated, but any reference to it is lost. You can consider this a memory leak. 

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

An alternative solution would be to return the value of `out`from `function`, so the caller could still use the memory referenced. On the other hand the caller also then takes the responsibility to free the 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;
}

```