10
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

1
  • 6
    This is one of the reasons that you should switch to getaddrinfo, which is the modern version of gethostbyname. After calling getaddrinfo, you have to freeaddrinfo. Jul 18, 2012 at 15:28

5 Answers 5

5

The man page you link to holds the answer:

When non-NULL, the return value may point at static data, see the notes below.

And a little later:

The functions gethostbyname() and gethostbyaddr() may return pointers to static data, which may be overwritten by later calls.

3

Supposing that an implementation wants to to handle arbitrarily large lists of addresses, it could do something like this:

struct hostent *gethostbyname(const char *name) {
    static struct hostent *results = 0;
    static size_t resultsize = 0;
    size_t count = get_count_of_addresses(name)
    if (count > resultsize) {
        struct hostent *tmp = realloc(results, N * count + M);
        if (tmp) {
            results = tmp;
            resultsize = count;
        } else {
            // handle error, I can't remember what the docs say
        }
    }
    fill_in_hostent(results, name);
    return results;
};

Optionally, the sockets library could do something to free results on exit (such as install an atexit handler), 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.

2

It may point to static memory. You'll need to do a deep copy of that if you want to retain multiple results. Not a shallow copy, because that structure itself contains pointers.

Beware of thread-safety.

2
  • As I pointed out in the OP, the field of h_add_list in struct hostent has variant length. That means the static struct has to be allocated enough space to hold potential long lists of IP.
    – q0987
    Jul 18, 2012 at 15:11
  • That's probably true, but note the documentation, and my answer, say "may" be static. There might be circumstances where the struct is dynamically created to accommodate an exceptional length, but uses static memory for typical cases and performance. Jul 18, 2012 at 15:30
1

It probably points to static memory, i.e. it's the same pointer for every call.

0

MS tells us that https://msdn.microsoft.com/en-us/library/windows/desktop/ms738524%28v=vs.85%29.aspx

The memory for the hostent structure returned by the 
gethostbyname function is allocated internally by the 
Winsock DLL from thread local storage. Only a single 
hostent structure is allocated and used, no matter how 
many times the gethostbyaddr or gethostbyname functions 
are called on the thread

So it would be thread safe on Windows, but...

It's been dropped from POSIX, and man7.org tells us that on Linux the hostname locale is not thread safe. http://man7.org/linux/man-pages/man3/gethostbyname.3.html

..and MS tells us that

The gethostbyname function has been deprecated 
by the introduction of the getaddrinfo function

Unfortunately, the replacement (getaddrinfo, thread safe on most platforms) was not part of sockets 1.x, and is not available on old platforms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.