This is the mail archive of the
libc-hacker@sourceware.cygnus.com
mailing list for the glibc project.
Re: malloc(0)
- To: zack@rabi.phys.columbia.edu
- Subject: Re: malloc(0)
- From: tb@MIT.EDU (Thomas Bushnell, n/BSG)
- Date: Mon, 1 Jun 1998 13:11:33 -0400
- Cc: libc-hacker@cygnus.com
Date: Mon, 01 Jun 1998 09:54:21 -0400
From: Zack Weinberg <zack@rabi.phys.columbia.edu>
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.
I would prefer to return a non-null pointer, because then we can
guarantee that malloc(N) != malloc(N) for two different calls to
malloc. That's a nice invariant to have.
Frequently it's handy to have loops in which the bottom case is a
zero-length malloc and have it Just Work. This would break such
situations.
Could you describe what `peculiar characteristics' a malloc of zero
length has?
Thomas
- References:
- malloc(0)
- From: Zack Weinberg <zack@rabi.phys.columbia.edu>