From gethostbyname(3) - Linux manual
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.
I've written programs that make multiple calls to gethostbyname and haven't had anything break because of overwriting of static data.
Could I ask for an example when multiple calls to gethostbyname would overwrite this static data?
It will be a problem when you do something like this:
The output will be
which is wrong, as the official host name of
www.google.comiswww.google.comand notyoutube-ui.l.google.com. The problem is thatgoogleandyoutubepoint at the same location (as you can see from thesame? yesoutput), so the information aboutwww.google.comis lost when you executegethostbynameagain.If you however do
then the output will be
So as long as you process the
hostentpointer of the firstgethostbynamecall before you do the second call, you will be fine.