I have recently been learning socket programming in C and I came across gethostbyname() function which returns struct hostent. Two of its fields are pointers to an array of pointers terminated by NULL pointer:
char** h_addr_list
char** h_aliases
I assume that this is dynamically allocated memory and if so then the question emerges - how is that memory released? There is no mention in the documentation about requirement resting on the function caller to release it.
On the other hand inet_ntoa returns statically stored char array defined within its body and memory leak is not an issue.
I appreciate your help to understand that memory management kind of topic.
On my system, the man page says
RETURN VALUE
The gethostbyname() and gethostbyaddr() functions return the hostent
structure or a null pointer if an error occurs. On error, the h_errno
variable holds an error number. When non‐NULL, the return value may
point at static data, see the notes below.
and
NOTES
The functions gethostbyname() and gethostbyaddr() may return pointers
to static data, which may be overwritten by later calls. Copying the
struct hostent does not suffice, since it contains pointers; a deep
copy is required.
This phrase pointer to static data
implies that you should not (nay, must not) call free()
on such pointers.