This is the mail archive of the libc-alpha@cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[Andrew Pimlott <pimlott@abel.MATH.HARVARD.EDU>] realloc(p, 0) vs. free


Hi,

Andrew reported the appended problem and accoring to Unix98 realloc
(p,0) should free the pointer p but we don't do.  

ISO C 9X defines realloc (ptr,0) as:
       If size is zero and ptr is not a null pointer, the object it
       points to is freed.

malloc.c has the following comment:
/*
  REALLOC_ZERO_BYTES_FREES should be set if a call to
  realloc with zero bytes should be the same as a call to free.
  Some people think it should. Otherwise, since this malloc
  returns a unique pointer for malloc(0), so does realloc(p, 0).
*/

and REALLOC_ZERO_BYTES_FREES is not set with glibc2.

Btw. malloc (0) is defined by Unix98 as:
If the size of the space requested is 0, the behaviour is
implementation-dependent; the value returned will be either a null
pointer or a unique pointer.

I'm in favor of defining  REALLOC_ZERO_BYTES_FREES to follow
Unix98/ISO C9x.

Andreas

1999-04-28  Andreas Jaeger  <aj@arthur.rhein-neckar.de>

	* malloc/malloc.c (REALLOC_ZERO_BYTES_FREES): Define it to follow
	ISO C9x and Unix98.

--- malloc/malloc.c.~1~	Wed Feb 24 18:50:16 1999
+++ malloc/malloc.c	Wed Apr 28 18:53:02 1999
@@ -1,5 +1,5 @@
 /* Malloc implementation for multiple threads without lock contention.
-   Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
+   Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Wolfram Gloger <wmglo@dent.med.uni-muenchen.de>
    and Doug Lea <dl@cs.oswego.edu>, 1996.
@@ -373,7 +373,7 @@
 */
 
 
-/*   #define REALLOC_ZERO_BYTES_FREES */
+#define REALLOC_ZERO_BYTES_FREES
 
 
 /*




Topics:
   realloc(p, 0) vs. free(0)


----------------------------------------------------------------------

Date: Wed, 28 Apr 1999 11:27:25 -0400 (EDT)
From: Andrew Pimlott <pimlott@abel.MATH.HARVARD.EDU>
To: glibc-linux@ricardo.ecn.wfu.edu
Subject: realloc(p, 0) vs. free(0)
Message-ID: <Pine.SOL.3.91.990428110320.540B-100000@abel.math.harvard.edu>
Content-Type: TEXT/PLAIN; charset=US-ASCII

I have found that realloc(p, 0) is not equivalent to free(0), and I would 
like to know whether this is a bug.

According to several documents (including the "vintage" Linux man page,
the Solaris 2.5.1 manpage, and the Single UNIX specification at
http://www.opengroup.org/onlinepubs/007908799/xsh/realloc.html),
realloc(p, 0) frees the memory at pointer p.  The glibc info page, however,
does not say this. 

The following program leaks memory on two Linux machines (with glibc 2.0.7
and 2.1.1).  The rate is 16 bytes per iteration.  Interestingly, it also
leaks on Solaris 2.5.1.  It does not leak when realloc(foo, 0) is replaced
with free(foo).  Be careful when running it not to fill up all your memory!

Whether or not this is technically a bug, completely freeing the memory 
on realloc(p, 0) seems the obviously preferred behavior.  Is there any 
reason for keeping the current behavior?

- ------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    void *foo;
    int i = 0;

    while (i++ < 1024 * 1024) {
        foo = malloc(100);
        if (foo == 0) {
            fprintf(stderr, "malloc failed!\n");
            exit(1);
        }
//        free(foo);
        foo = realloc(foo, 0);
        if (foo == 0) {
            fprintf(stderr, "realloc failed\n");
            exit(1);
        }
//        sleep(1);
    }
}
- ------

Andrew


------------------------------

End of forwardsACpzr Digest
***************************



-- 
 Andreas Jaeger   aj@arthur.rhein-neckar.de    jaeger@informatik.uni-kl.de
  for pgp-key finger ajaeger@aixd1.rhrk.uni-kl.de

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]