Your challenge is that something needs to release the resources (i.e. cause the free()
to happen).
Normally, the caller frees the allocated memory either by calling free()
directly (see how strdup
users work for instance), or by calling a function you provide the wraps free
. You might, for instance, require callers to call a foo_destroy
function. As another poster points out you might choose to wrap that in an opaque struct
, though that's not necessary as having your own allocation and destroy functions is useful even without that (e.g. for resource tracking).
However, another way would be to use some form of clean-up function. For instance, when the string is allocated, you could attach it to a list of resources allocated in a pool, then simply free the pool when done. This is how apache2
works with its apr_pool
structure. In general, you don't free()
anything specifically under that model. See here and (easier to read) here.
What you can't do in C (as there is no reference counting of malloc()
d structures) is directly determine when the last 'reference' to an object goes out of scope and free it then. That's because you don't have references, you have pointers.
Lastly, you asked how existing functions return char *
variables:
Some (like
strdup
,get_current_dir_name
andgetcwd
under some circumstances) expect the caller to free.Some (like
strerror_r
andgetcwd
in under other circumstances) expect the caller to pass in a buffer of sufficient size.Some do both: from the
getcwd
man page:
As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc)
getcwd()
allocates the buffer dynamically usingmalloc(3)
ifbuf
isNULL
. In this case, the allocated buffer has the lengthsize
unlesssize
is zero, whenbuf
is allocated as big as necessary. The caller shouldfree(3)
the returned buffer.
Some use an internal static buffer and are thus not reentrant / threadsafe (yuck - do not do this). See
strerror
and whystrerror_r
was invented.Some only return pointers to constants (so reentrancy is fine), and no free is required.
Some (like
libxml
) require you to use a separatefree
function (xmlFree()
in this case)Some (like
apr_palloc
) rely on the pool technique above.