home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / aix / 13148 < prev    next >
Encoding:
Text File  |  1993-01-08  |  2.6 KB  |  57 lines

  1. Newsgroups: comp.unix.aix
  2. 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
  3. From: dcm@codesmith.austin.ibm.com (Craig Miller - dcm@austin.ibm.com)
  4. Subject: Re: malloc(0) fails on AIX and nowhere else I could find ...
  5. Sender: news@austin.ibm.com (News id)
  6. Message-ID: <C0JvG9.1HBs@austin.ibm.com>
  7. Date: Fri, 8 Jan 1993 19:28:57 GMT
  8. References: <93Jan7.012526est.169557-2@watdragon.uwaterloo.ca> <C0HCs4.Eoy@axion.bt.co.uk> <1ikdebINN6qv@CS.UTK.EDU>
  9. Organization: IBM AWD, Austin
  10. Lines: 45
  11.  
  12. In article <1ikdebINN6qv@CS.UTK.EDU> moore@cs.utk.edu writes:
  13. >...
  14. >Say you're implementing a strings package and your string descriptor
  15. >looks like struct string { int count; char *ptr }; Now when you assign
  16. >to a string you want to malloc (or realloc) the right # of bytes and
  17. >assign the return value from malloc to the ptr.  But with AIX (and
  18. >many other SysV-based systems), you have to explicitly test in case
  19. >the string you want to assign is zero length.
  20.  
  21. those who support malloc(0):    ptr = malloc(length);
  22. those who don't:        ptr = (length == 0) ? NULL : malloc(length);
  23.  
  24. Right?  And the second case *always* works, regardless of whether the
  25. local implementation of malloc supports malloc(0) or not.  Right?
  26.  
  27. So the second case is portable.  Right?
  28.  
  29. Five or six years ago, when I was developing applications, I wrote all
  30. my apps the second way.  I realized that malloc(0) had basically been
  31. undefined since day one.  Wouldn't it be more portable and easier to
  32. simply design your application this way instead of complaining each time
  33. you try to port your application to a new box? 
  34.  
  35. >Saying you shouldn't be able to malloc() zero bytes is like saying you
  36. >shouldn't be able to have a zero-length file, or an empty record in a
  37. >file, or you shouldn't be able to multiply something by zero.
  38.  
  39. I disagree.  Saying you shouldn't be able to malloc() zero bytes is
  40. similiar to saying you can't strcpy() NULL: the behavior is undefined.
  41. They've always been undefined!  Why depend on some implementation-specific
  42. behavior when C gives you the opportunity to do otherwise?
  43.  
  44. >To be fair, AIX isn't unique in this regard -- most SysV based systems
  45. >work this way.  Like most of the features unique to SysV, this one is
  46. >a botch.
  47.  
  48. To most BSD users, SysV is a botch! (I used BSD in college and was a
  49. BSD bigot until I was forced to use SysV...  And forced to realize
  50. what it took to write BSD/SysV portably programs)
  51.  
  52.     Craig
  53. -- 
  54. Craig Miller, software mechanic:  AIXV3 Change Team (Level3 support)
  55. #include <ibm/stds/disclaimer.h>
  56. char *internet = "dcm@austin.ibm.com", *vnet = "AUSVM6(TKG007)";
  57.