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.
source share