Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!unmvax!pprg.unm.edu!hc!lll-winken!uunet!sdrc!scjones From: scjones@sdrc.UUCP (Larry Jones) Newsgroups: comp.std.c Subject: Re: realloc and malloc and zero-sized objects Message-ID: <694@sdrc.UUCP> Date: 5 Apr 89 21:06:35 GMT References: <10170@bloom-beacon.MIT.EDU> <1989Apr5.000608.16582@sq.com> Organization: Structural Dynamics Research Corp., Cincinnati Lines: 41 In article <1989Apr5.000608.16582@sq.com>, msb@sq.com (Mark Brader) writes: > [Much interesting text deleted - see original if interester] > > Since 0 is not a possible size for an object, the invocation malloc(0) > falls under the general rule [for out-of-range arguments to > library functions] and is undefined behavior. It would of > course be permissible for an implementation to define its behavior in > this circumstance; this would be an extension to the language. This would be true except for the statement in 4.10.3 Memory Management Functions: "If the size of the space requested is zero, the behavior is implementation-defined; the value returned shall be either a null pointer or a unique pointer." Thus, even though there ain't no such thing as a zero sized object, malloc and friends are allowed to return non-null pointers when given a size of zero. > # Returns > # The realloc function returns either a null pointer or a pointer to the > # possibly moved allocated space. > > Note the either-or. Since a null pointer cannot point to allocated space, > this is really an exclusive or. Since an object cannot have zero size > and also because of the last sentence in the Description section, it is > clear that realloc is *not* allocating any space if it is called as > realloc(p,0) where p is a valid non-null pointer. Therefore realloc *must* > return a null pointer. But the statement above gives realloc the liberty to allocate space if it wants to, so whether it returns a null pointer or not is implementation defined as I said originally. ---- Larry Jones UUCP: uunet!sdrc!scjones SDRC scjones@sdrc.UU.NET 2000 Eastman Dr. BIX: ltl Milford, OH 45150 AT&T: (513) 576-2070 "When all else fails, read the directions."