Now there is a question:
In the example, it declares struct addrinfo *ailist, *aip;
And ailist is used in a for loop:
for (aip = ailist; aip != NULL; aip = aip->ai_next) {
...
printf("\n host: \t%s", aip->ai_canonname?aip->ai_canonname:"-");
...
}
But where is the memory aip pointer to, in the for loop?
Does it call malloc to allocate memory for the list of struct addrinfo?
If so, in the example, it doesn't call freeaddrinfo() to free the allocated memory, and so does in the later examples, which is thought to cause memory leakage.
Can anyone help me?
Memory is allocated in the call to getaddrinfo, ailist is assigned the address of that allocated memory, and aip is a copy of ailist. When no longer needed that memory should be freed!