Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Rainer Weikusat Newsgroups: comp.unix.programmer Subject: Re: the usage of getaddrinfo() Date: Wed, 24 Aug 2011 13:09:28 +0100 Lines: 65 Message-ID: <87ty97m2o7.fsf@sapphire.mobileactivedefense.com> References: <3e76da03-f115-45da-a1d3-fb3b4d35de56@glegroupsg2000goo.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: individual.net xwUKAiC+5F72ZfhgNkZnqgbej+TlE9bkigWfDqV2xIosupVWU= Cancel-Lock: sha1:/tjW55rmGRqydslxfVyCil9EVK0= sha1:Mtd5I9CdkvptNYAsuW/9sQp7pBU= User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Xref: x330-a1.tempe.blueboxinc.net comp.unix.programmer:1186 wy 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).