Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.unix.programmer > #1186
| From | Rainer Weikusat <rweikusat@mssgmbh.com> |
|---|---|
| Newsgroups | comp.unix.programmer |
| Subject | Re: the usage of getaddrinfo() |
| Date | 2011-08-24 13:09 +0100 |
| Message-ID | <87ty97m2o7.fsf@sapphire.mobileactivedefense.com> (permalink) |
| References | <3e76da03-f115-45da-a1d3-fb3b4d35de56@glegroupsg2000goo.googlegroups.com> |
wy <warmyouth@gmail.com> writes:
> Hello, in APUE (Advanced Programming in the UNIX Environment), P559, Figure 16.8, appears the following code:
>
> int
> main(int argc, char *argv[])
> {
> struct addrinfo *ailist, *aip;
> struct addrinfo hint;
> struct sockaddr_in *sinp;
> const char *addr;
> int err;
> char abuf[INET_ADDRSTRLEN];
>
> if (argc != 3)
> err_quit("usage: %s nodename service", argv[0]);
> hint.ai_flags = AI_CANONNAME;
> hint.ai_family = 0;
> hint.ai_socktype = 0;
> hint.ai_protocol = 0;
> hint.ai_addrlen = 0;
> hint.ai_canonname = NULL;
> hint.ai_addr = NULL;
> hint.ai_next = NULL;
> if ((err = getaddrinfo(argv[1], argv[2], &hint, &ailist)) != 0)
> err_quit("getaddrinfo error: %s", gai_strerror(err));
> for (aip = ailist; aip != NULL; aip = aip->ai_next) {
> print_flags(aip), printf("\t");
> print_family(aip), printf("\t");
> print_type(aip), printf("\t");
> print_protocol(aip), printf("\t");
> printf("\n host: \t%s", aip->ai_canonname?aip->ai_canonname:"-");
> if (aip->ai_family == AF_INET) {
> sinp = (struct sockaddr_in *)aip->ai_addr;
> addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf,
> INET_ADDRSTRLEN);
> printf("\n address:\t%s", addr?addr:"unknown"), printf("\t");
> printf("\n port:\t%d", ntohs(sinp->sin_port)), printf("\t");
> }
> printf("\n");
> }
> exit(0);
> }
[...]
> 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?
getaddrinfo does that and stores a pointer to the first addrinfo
structure of the list in the location pointed to by its last argument.
> 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.
freeaddrinfo (or free in general) informs the userspace memory allocator (in
the C library) that a specific memory area is no longer needed by the
application and can thus be reused to satisfy future memory allocation
requests. But if there won't be any 'future memory allocation
requests', for instance because, as in the example above, the program will terminate
after printing the information returned by getaddrinfo, there is no
point in doing this (any physical memory committed to a process will
become 'free for reuse by the kernel' after the process has
terminated).
Back to comp.unix.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
the usage of getaddrinfo() wy <warmyouth@gmail.com> - 2011-08-24 00:43 -0700
Re: the usage of getaddrinfo() Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-24 13:09 +0100
Re: the usage of getaddrinfo() wy <warmyouth@gmail.com> - 2011-08-26 01:25 -0700
Re: the usage of getaddrinfo() David Schwartz <davids@webmaster.com> - 2011-08-26 02:54 -0700
Re: the usage of getaddrinfo() John Gordon <gordon@panix.com> - 2011-08-26 13:44 +0000
Re: the usage of getaddrinfo() Rick Jones <rick.jones2@hp.com> - 2011-08-26 21:41 +0000
Re: the usage of getaddrinfo() Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-28 17:18 +0100
Re: the usage of getaddrinfo() John Gordon <gordon@panix.com> - 2011-08-24 17:29 +0000
csiph-web