Re: memory leak in getprotobyname()
On Tue, Dec 02, 2003 at 09:59:46AM -0500, Roman Fleysher wrote:
>
> I am not an expert on the subject, but it looks as if getprotobyname()
> allocates a structure and returns a pointer to it. Thus, each time you
> call it, new structure is allocated and new pointer is returned. You can
> check it with
>
> if(getprotobyname("tcp") != getprotobyname("tcp"))
> printf("Each call allocates new structure
> (assuming these were not NULL's)\n");
That's incorrect. From the manual:
You can use `getprotobyname' and `getprotobynumber' to search the
protocols database for a specific protocol. The information is
returned in a statically-allocated structure; you must copy the
information if you need to save it across calls.
>
> Roman
>
> > ========================8<============================
> > #include <netdb.h>
> > #include <stdio.h>
> >
> > int main(void) {
> > int i;
> > struct protoent *protocol=NULL;
> >
> > for (i=0; i<200000; i++) {
> > protocol=getprotobyname("tcp");
> > }
> > printf("\n RESULT : %02x \n",protocol->p_proto);
> >
> > return 0;
> > }
> > ========================8<============================
>
>
>
> --
> To UNSUBSCRIBE, email to debian-glibc-request@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
>
>
--
Daniel Jacobowitz
MontaVista Software Debian GNU/Linux Developer
Reply to: