Re: NEVER USE return str_dup(s); !!!

From: Scatter (
Date: 09/30/95

> > Maybe str_dup()? (its defined in utils.c)
> Don't even think about doing that !!!!!!!!!!!!
> This is the classical example of a memory leak !
> Everytime you pass this point you will have some bytes of
> memory less and some garbage more in memory until you crash.

Excerpt from a man page follows:
       strdup - duplicate a string

       #include <string.h>

       char *strdup(const char *s);

       The  strdup()  function  returns a pointer to a new string
       which is a duplicate of the string s.  Memory for the  new
       string  is  obtained with malloc(3), and can be freed with

       The strdup() function returns a pointer to the  duplicated
       string, or NULL if insufficient memory was available.

       ENOMEM Insufficient memory available to allocate duplicate
end of man page excerpt.

How on earth does returning a strdup create a memory leak?  If value
is freed when your done with it, there is no leak.  This call seems to 
be just an enhanced malloc....

Not being argumentative, I just don't understand.

This archive was generated by hypermail 2b30 : 12/07/00 PST