home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.aix
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!geraldo.cc.utexas.edu!portal.austin.ibm.com!awdprime.austin.ibm.com!codesmith.austin.ibm.com!dcm
- From: dcm@codesmith.austin.ibm.com (Craig Miller - dcm@austin.ibm.com)
- Subject: Re: malloc(0) fails on AIX and nowhere else I could find ...
- Sender: news@austin.ibm.com (News id)
- Message-ID: <C0JvG9.1HBs@austin.ibm.com>
- Date: Fri, 8 Jan 1993 19:28:57 GMT
- References: <93Jan7.012526est.169557-2@watdragon.uwaterloo.ca> <C0HCs4.Eoy@axion.bt.co.uk> <1ikdebINN6qv@CS.UTK.EDU>
- Organization: IBM AWD, Austin
- Lines: 45
-
- In article <1ikdebINN6qv@CS.UTK.EDU> moore@cs.utk.edu writes:
- >...
- >Say you're implementing a strings package and your string descriptor
- >looks like struct string { int count; char *ptr }; Now when you assign
- >to a string you want to malloc (or realloc) the right # of bytes and
- >assign the return value from malloc to the ptr. But with AIX (and
- >many other SysV-based systems), you have to explicitly test in case
- >the string you want to assign is zero length.
-
- those who support malloc(0): ptr = malloc(length);
- those who don't: ptr = (length == 0) ? NULL : malloc(length);
-
- Right? And the second case *always* works, regardless of whether the
- local implementation of malloc supports malloc(0) or not. Right?
-
- So the second case is portable. Right?
-
- Five or six years ago, when I was developing applications, I wrote all
- my apps the second way. I realized that malloc(0) had basically been
- undefined since day one. Wouldn't it be more portable and easier to
- simply design your application this way instead of complaining each time
- you try to port your application to a new box?
-
- >Saying you shouldn't be able to malloc() zero bytes is like saying you
- >shouldn't be able to have a zero-length file, or an empty record in a
- >file, or you shouldn't be able to multiply something by zero.
-
- I disagree. Saying you shouldn't be able to malloc() zero bytes is
- similiar to saying you can't strcpy() NULL: the behavior is undefined.
- They've always been undefined! Why depend on some implementation-specific
- behavior when C gives you the opportunity to do otherwise?
-
- >To be fair, AIX isn't unique in this regard -- most SysV based systems
- >work this way. Like most of the features unique to SysV, this one is
- >a botch.
-
- To most BSD users, SysV is a botch! (I used BSD in college and was a
- BSD bigot until I was forced to use SysV... And forced to realize
- what it took to write BSD/SysV portably programs)
-
- Craig
- --
- Craig Miller, software mechanic: AIXV3 Change Team (Level3 support)
- #include <ibm/stds/disclaimer.h>
- char *internet = "dcm@austin.ibm.com", *vnet = "AUSVM6(TKG007)";
-