Re: malloc(0) returns NULL

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
Date: Sunday, February 2, 1992 - 11:46 am

In article <1992Feb1.153354.1@ttd.teradyne.com> nichols@ttd.teradyne.com writes:

Well, finally at least one person agrees with me :-)

Here's another example of why I find it annoying that malloc(0) is treated as
a special case of malloc(N) - it makes it more difficult to cleanly handle
arrays whose size is computed at run time. Consider:

/* process an array of N `Things' */
function(Thing x[], int N)
   {
   /* say we need to stash away a copy of x[] for awhile */
   Thing *stash = malloc(N * sizeof(Thing));
   
   /* check for out of memory (oops, must handle special case of N==0) */
   if (!stash && N) error();

   /* fill the array */
   for (i = 0; i < N; ++i) stash[i] = x[i];
   
   /* then sometime later (oops, must handle special case of N==0) */
   if (stash) free(stash);
   }

Ok, I'll stop beating a dead horse. Judging from some of the (surprisingly
obnoxious) messages in my mailbox, I seem to be in the minority on this
issue. 

flame on...
Some people seem to feel that any mail originating from an IBM gateway must 
be from some guy who just climbed off a 370 mainframe. Hey, believe it or 
not, there ARE people here who grew up on unix (real unix, not just AIX),
love it as much as you do, and depend on it for all their work.
So don't pre-judge ok?
...flame off

-- 
Derek Lieber
derek@watson.ibm.com
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: malloc(0) returns NULL, Derek Lieber, (Sun Feb 2, 11:46 am)