This is the mail archive of the
libc-hacker@sourceware.cygnus.com
mailing list for the glibc project.
malloc(0)
- To: libc-hacker@cygnus.com
- Subject: malloc(0)
- From: Zack Weinberg <zack@rabi.phys.columbia.edu>
- Date: Mon, 01 Jun 1998 09:54:21 -0400
There's been some discussion recently in comp.std.c about what
malloc(0) and realloc(x, 0) are supposed to do. The standard allows
returning some non-null pointer in both cases, which is what we
currently do. However, that non-null pointer has peculiar
characteristics and it seems to me that it would be less surprising if
both these operations returned NULL.
Proposed patch follows. Comment?
zw
============================================================
Index: malloc/malloc.c
--- malloc/malloc.c Sat, 30 May 1998 16:12:00 -0400 zack Z.7
+++ malloc/malloc.c Mon, 01 Jun 1998 09:51:44 -0400 zack Z.7(w)
@@ -373,7 +373,7 @@
*/
-/* #define REALLOC_ZERO_BYTES_FREES */
+#define REALLOC_ZERO_BYTES_FREES
/*
@@ -2586,6 +2586,13 @@
}
#endif
+#ifdef REALLOC_ZERO_BYTES_FREES
+ /* zw: for consistency, in this case make malloc(0) do nothing
+ and return NULL. */
+ if (bytes == 0)
+ return 0;
+#endif
+
nb = request2size(bytes);
arena_get(ar_ptr, nb);
if(!ar_ptr)