Just for people to think about...

In working with object oriented code, we OFTEN create and return zero
length arrays when we are returning an array and have nothing to return
(ex: zero records from a database fetch.)

Accepting a zero length request, and returning a zero byte long
allocation makes plenty of sense.

Why would you ask for a zero length memory block? Well, perhaps
something else didn't match, and you'll now loop zero times.

A pointer to a block of length zero is valid, and should not terminate
the program. (my opinion)

Russ Allbery wrote:
> 
> Donald Sharp <[EMAIL PROTECTED]> writes:
> 
> > Although looking at the xmalloc call, I don't know if it's such a good
> > thing for it to take a 0 length request and turn it into a 1 byte
> > request.  Why would we need to ask for a 0 length portion of memory?
> 
> malloc of 0 bytes on some platforms returns NULL.  xmalloc never returns
> NULL.  This is the entire point and purpose of xmalloc; you call xmalloc
> because you don't ever want to deal with malloc returning NULL pointers.
> Either you get a valid pointer back or the program terminates.
> 
> Allowing xmalloc to return NULL even for a special case would, in my
> opinion, violate a key portion of its semantics.
> 
> --
> Russ Allbery ([EMAIL PROTECTED])             <http://www.eyrie.org/~eagle/>

Reply via email to