I've just started to work with UDP sockets in C and I have a question relating to memory management. Often examples show something of the following
struct addrinfo *result; //to store results
struct addrinfo hints; //to indicate information we want
:
:
:
if( (s = getaddrinfo(hostname, NULL, &hints, &result)) != 0){
fprintf(stderr, "getaddrinfo: %s\n",gai_strerror(s));
exit(1);
}
:
:
:
//free the addrinfo struct
freeaddrinfo(result);
I've also seen the introduction of an additonal local addrinfo
structure used in a for
loop to cycle through the result
for the sake of extracting a particular entry...something like this
struct addrinfo *entry; //to store a result from linked list
:
:
:
for (entry = result; entry != NULL; entry = entry->ai_next)
{
:
:
:
}
My question is why is result
the only structure that gets passed to freeaddrinfo
and not hints
or entry
? In other words, I've never seen anyone call freeaddrinfo(hints)
.
One other thing, should I be preallocating memory for or initializing structures of the form addrinfo
? If so, how do I do that?