Apollo
Apollo

Reputation: 9054

asprintf(); causing memory issues

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

Answers (1)

jxh
jxh

Reputation: 70392

Does free() need to be called for every asprintf()?

Yes. As per its documentation:

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.

Upvotes: 2

Related Questions