Reputation: 9054
I'm using asprintf(&somestring,"%s%s",stringone,stringtwo)
to concatenate strings. It works fine if I call it once and then free(somestring)
once. However, I run into problems if I call asprintf()
multiple times but only free(somestring)
once in a recursive function. Does free()
need to be called for every asprintf()
?
Upvotes: 0
Views: 416
Reputation: 70392
Does
free()
need to be called for everyasprintf()
?
Yes. As per its documentation:
The functions
asprintf()
andvasprintf()
are analogs ofsprintf(3)
andvsprintf(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.
Upvotes: 2