malloc(0)
Zack Weinberg
zack@rabi.phys.columbia.edu
Mon Jun 1 06:55:00 GMT 1998
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
Mon Jun 1 09:54:24 1998 Zack Weinberg <zack@rabi.phys.columbia.edu>
* malloc/malloc.c: Define REALLOC_ZERO_BYTES_FREES.
(mALLOc): Return 0 if R_Z_B_F is defined and bytes == 0.
============================================================
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)
More information about the Libc-hacker
mailing list