struct hostent *gethostbyname(const char *name)
Note that hostent.h_addr_list is a field with variant length.
How does the function gethostbyname have the implementation that returns a pointer pointing to a struct but doesn’t require the caller to release the resource?
All examples used in the famous book Unix Network Programming Vol 1 by R. Stevens do not contain code to release those returned pointers and I assume that these are not ignorance. Also one example from MSDN does the same thing example of usage
Supposing that an implementation wants to to handle arbitrarily large lists of addresses, it could do something like this:
Optionally, the sockets library could do something to free
resultson exit (such as install anatexithandler), to avoid debugging tools reporting a memory leak.I’ve ignored the possibility that the count of addresses could change in between sizeing the structure and filling it in — in practice you’d get the DNS result back and then do stuff with it, so that would not be possible. I’ve left it as two separate calls to avoid introducing a pseudo-code representation for the DNS result.