home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / aix / 13206 < prev    next >
Encoding:
Internet Message Format  |  1993-01-11  |  3.9 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!cs.utk.edu!cs.utk.edu!moore
  2. From: moore@cs.utk.edu (Keith Moore)
  3. Newsgroups: comp.unix.aix
  4. Subject: Re: malloc(0) fails on AIX and nowhere else I could find ...
  5. Date: 12 Jan 1993 00:33:40 GMT
  6. Organization: Univ. of Tenn. Computer Science, Knoxville
  7. Lines: 77
  8. Distribution: world
  9. Message-ID: <1it3l4INNfh1@CS.UTK.EDU>
  10. References: <93Jan7.012526est.169557-2@watdragon.uwaterloo.ca> <C0HCs4.Eoy@axion.bt.co.uk> <1ikdebINN6qv@CS.UTK.EDU> <C0JvG9.1HBs@austin.ibm.com>
  11. Reply-To: moore@cs.utk.edu
  12. NNTP-Posting-Host: wilma.cs.utk.edu
  13.  
  14. In article <C0JvG9.1HBs@austin.ibm.com>, dcm@codesmith.austin.ibm.com (Craig Miller - dcm@austin.ibm.com) writes:
  15. > In article <1ikdebINN6qv@CS.UTK.EDU> moore@cs.utk.edu writes:
  16. > >...
  17. > >Say you're implementing a strings package and your string descriptor
  18. > >looks like struct string { int count; char *ptr }; Now when you assign
  19. > >to a string you want to malloc (or realloc) the right # of bytes and
  20. > >assign the return value from malloc to the ptr.  But with AIX (and
  21. > >many other SysV-based systems), you have to explicitly test in case
  22. > >the string you want to assign is zero length.
  23.  
  24.  
  25. > those who support malloc(0):    ptr = malloc(length);
  26. > those who don't:        ptr = (length == 0) ? NULL : malloc(length);
  27. > Right?  And the second case *always* works, regardless of whether the
  28. > local implementation of malloc supports malloc(0) or not.  Right?
  29. >
  30. > So the second case is portable.  Right?
  31.  
  32. No.  I cannot portably call realloc() or free() with the NULL pointer
  33. returned by malloc(0) on SysV systems.  [If I could do that, I
  34. wouldn't ever need a malloc() function; I'd always call
  35. realloc(NULL,size), which would simplify a lot of code that does
  36. resizing of dynamic structures.  POSIX defines realloc to work this
  37. way, but not all UNIXen do.]
  38.  
  39. To make your approach portable, I'd have to do this when reallocing :
  40.  
  41. newptr = ptr ? realloc (ptr, newsize) : malloc (newsize);
  42.  
  43. Instead, I do this:
  44.  
  45. #define xmalloc(x) malloc((x) == 0 ? 1 : (x))
  46.  
  47. which gives me a return value that I can portably pass to free() and
  48. realloc().
  49.  
  50. > Five or six years ago, when I was developing applications, I wrote all
  51. > my apps the second way.  I realized that malloc(0) had basically been
  52. > undefined since day one.  Wouldn't it be more portable and easier to
  53. > simply design your application this way instead of complaining each time
  54. > you try to port your application to a new box?
  55.  
  56. Of course I write my code to check for malloc(0).  My point is, if
  57. SysV weren't broken, I wouldn't have to.  
  58.  
  59. I keep complaining about it because the example is instructive to
  60. others.  Of course, the example is lost on some people -- perhaps the
  61. same people who think that malloc() shouldn't actually have to
  62. allocate backing storage....
  63.  
  64. malloc(0) *was* originally defined (at least in V7) by someone who had
  65. enough of a clue to realize that it should return something that could
  66. be free'd and realloc'ed.
  67.  
  68. How many bugs in application has this unfortunate definition of
  69. malloc() caused?  How many extra lines of code have had to be written
  70. to get around the design flaw?  What is the cost in development and
  71. maintenance of all of this extra code?
  72.  
  73. > >Saying you shouldn't be able to malloc() zero bytes is like saying you
  74. > >shouldn't be able to have a zero-length file, or an empty record in a
  75. > >file, or you shouldn't be able to multiply something by zero.
  76. > I disagree.  Saying you shouldn't be able to malloc() zero bytes is
  77. > similiar to saying you can't strcpy() NULL: the behavior is undefined.
  78.  
  79. No.  It's like saying you shouldn't be able to strcat(foo, "").
  80.  
  81. When you say that malloc(0) is undefined -- that's the bug!  It's not
  82. meaningless to ask for a pointer to a block containing zero bytes of
  83. storage.  In fact, it's quite useful.
  84.  
  85. --
  86. Keith Moore / U.Tenn CS Dept / 107 Ayres Hall / Knoxville TN  37996-1301
  87. Internet: moore@cs.utk.edu      BITNET: moore@utkvx
  88. Let's stamp out FORTRAN in our lifetime.
  89.