11.8 freeaddrinfo
Function
All the storage returned by
getaddrinfo, the addrinfo structures, the
ai_addr structures, and the ai_canonname string
are obtained dynamically (e.g., from malloc). This storage
is returned by calling freeaddrinfo.
#include <netdb.h>
|
void freeaddrinfo (struct addrinfo
*ai);
|
ai should point
to the first addrinfo structure returned by
getaddrinfo. All the structures in the linked list are
freed, along with any dynamic storage pointed to by those
structures (e.g., socket address structures and canonical
hostnames).
Assume we call getaddrinfo, traverse
the linked list of addrinfo structures, and find the
desired structure. If we then try to save a copy of the information
by copying just the addrinfo structure and calling
freeaddrinfo, we have a lurking bug. The reason is that
the addrinfo structure itself points to dynamically
allocated memory (for the socket address structure and possibly the
canonical name), and memory pointed to by our saved structure is
returned to the system when freeaddrinfo is called and can
be used for something else.
Making a copy of just the addrinfo
structure and not the structures that it in turn points to is
called a shallow copy. Copying the
addrinfo structure and all the structures that it points
to is called a deep copy.
|