How to "free" variable after end of the function?

If you return allocated memory, then it is the caller responsibility to free it.

char *res;
res = someFunction("something 1", "something 2");
free(res);

Use free. In your case, it will be:

char* result = malloc(la + 2 * sizeof(char));
...
free (result);

Also, if you're returning allocated memory, like strdup does, the caller of your function has to free the memory. Like:

result = somefunction ();
...
free (result);

If you're thinking of freeing it after returning it, that is not possible. Once you return something from the function, it automatically gets terminated.


In the code that called someFunction.

You also have to make clear in the documentation (you have that, right?!), that the caller has to call free, after finished using the return value.