Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.programmer > #1185 > unrolled thread

the usage of getaddrinfo()

Started bywy <warmyouth@gmail.com>
First post2011-08-24 00:43 -0700
Last post2011-08-24 17:29 +0000
Articles 8 — 5 participants

Back to article view | Back to comp.unix.programmer


Contents

  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

#1185 — the usage of getaddrinfo()

Fromwy <warmyouth@gmail.com>
Date2011-08-24 00:43 -0700
Subjectthe usage of getaddrinfo()
Message-ID<3e76da03-f115-45da-a1d3-fb3b4d35de56@glegroupsg2000goo.googlegroups.com>
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);
}

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?

[toc] | [next] | [standalone]


#1186

FromRainer Weikusat <rweikusat@mssgmbh.com>
Date2011-08-24 13:09 +0100
Message-ID<87ty97m2o7.fsf@sapphire.mobileactivedefense.com>
In reply to#1185
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).

[toc] | [prev] | [next] | [standalone]


#1189

Fromwy <warmyouth@gmail.com>
Date2011-08-26 01:25 -0700
Message-ID<37573d22-5874-46c4-b354-695e04a50d53@glegroupsg2000goo.googlegroups.com>
In reply to#1186
So, if a process called malloc(), and forgeted to free the memory, 
then after the termination of the process, the memory will still be freed automatically?

[toc] | [prev] | [next] | [standalone]


#1190

FromDavid Schwartz <davids@webmaster.com>
Date2011-08-26 02:54 -0700
Message-ID<af7ce949-fd0a-4119-9f28-f68e095de30c@u7g2000pro.googlegroups.com>
In reply to#1189
On Aug 26, 1:25 am, wy <warmyo...@gmail.com> wrote:
> So, if a process called malloc(), and forgeted to free the memory,
> then after the termination of the process, the memory will still be freed automatically?

Do you mean physical memory or virtual memory?

In any event, it doesn't matter. There is no need to free the memory
since the memory was in an address space that no longer exists. The
'malloc' function doesn't really allocate physical memory but merely
arranges to map virtual memory into the process address space as
needed. So there is nothing to free, since there is longer any place
to map.

DS

[toc] | [prev] | [next] | [standalone]


#1192

FromJohn Gordon <gordon@panix.com>
Date2011-08-26 13:44 +0000
Message-ID<j387vv$cnm$1@reader1.panix.com>
In reply to#1189
In <37573d22-5874-46c4-b354-695e04a50d53@glegroupsg2000goo.googlegroups.com> wy <warmyouth@gmail.com> writes:

> So, if a process called malloc(), and forgeted to free the memory, 
> then after the termination of the process, the memory will still be
> freed automatically?

Yes.

(Assuming you are on a "normal" computer.  Computers that operate in an
embedded environment, such as the microchips in your television or
wristwatch, might behave differently.)

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#1193

FromRick Jones <rick.jones2@hp.com>
Date2011-08-26 21:41 +0000
Message-ID<j393u8$mq0$1@usenet01.boi.hp.com>
In reply to#1192
John Gordon <gordon@panix.com> wrote:
> In <37573d22-5874-46c4-b354-695e04a50d53@glegroupsg2000goo.googlegroups.com> wy <warmyouth@gmail.com> writes:

> > So, if a process called malloc(), and forgeted to free the memory, 
> > then after the termination of the process, the memory will still be
> > freed automatically?

> Yes.

Though, you should not rely on that too heavily.

If your program makes only the one getaddrinfo() call, not doing the
freeaddrinfo() is probably OK.  However, if your program might make
many getaddrinfo() calls and is going to run for some length of time,
your program could end-up reaching the limit of the memory it would be
allocated and crash if you don't do the freeaddrinfo() calls as you
go.

Just like malloc() and free().

rick jones
-- 
web2.0 n, the dot.com reunion tour...
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...

[toc] | [prev] | [next] | [standalone]


#1195

FromRainer Weikusat <rweikusat@mssgmbh.com>
Date2011-08-28 17:18 +0100
Message-ID<87liud7bnk.fsf@sapphire.mobileactivedefense.com>
In reply to#1193
Rick Jones <rick.jones2@hp.com> writes:
> John Gordon <gordon@panix.com> wrote:
>> In <37573d22-5874-46c4-b354-695e04a50d53@glegroupsg2000goo.googlegroups.com>
>> wy <warmyouth@gmail.com> writes:
>> > So, if a process called malloc(), and forgeted to free the memory, 
>> > then after the termination of the process, the memory will still be
>> > freed automatically?
>> Yes.
>
> Though, you should not rely on that too heavily.
>
> If your program makes only the one getaddrinfo() call, not doing the
> freeaddrinfo() is probably OK.  However, if your program might make
> many getaddrinfo() calls and is going to run for some length of time,
> your program could end-up reaching the limit of the memory it would be
> allocated and crash if you don't do the freeaddrinfo() calls as you
> go.

It is also generally a good idea to reuse memory which has already
been used instead of continuing to use 'fresh memory': Modern
computers rely heavily on cacheing in order to achieve good
performance both regarding memory content and memory addresses. Unless
you are an orthodox C++ (hundreds of M of RSS) or - heaven forbid - even a
Java (RSS ::= 'maximum size of the virtual address space'), where you
really don't need to worry about the efficiency of your code because
'all was already lost before you started', it makes sense to be
parsimonious with respect to memory usage even if there is no risk to
ever run out of it.

[toc] | [prev] | [next] | [standalone]


#1187

FromJohn Gordon <gordon@panix.com>
Date2011-08-24 17:29 +0000
Message-ID<j33cd5$oip$1@reader1.panix.com>
In reply to#1185
In <3e76da03-f115-45da-a1d3-fb3b4d35de56@glegroupsg2000goo.googlegroups.com> wy <warmyouth@gmail.com> writes:

> 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?

aip points to the ailist pointer, for which memory was allocated in
the call to getaddrinfo().

> 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.

All memory is freed when the program exits.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.unix.programmer


csiph-web