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

  1. Newsgroups: comp.unix.aix
  2. Path: sparky!uunet!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: <C0LnII.pGK@austin.ibm.com>
  7. Date: Sat, 9 Jan 1993 18:32:42 GMT
  8. References: <1ikdebINN6qv@CS.UTK.EDU> <C0JvG9.1HBs@austin.ibm.com> <4512@unisql.UUCP>
  9. Organization: IBM AWD, Austin
  10. Lines: 29
  11.  
  12. In article <4512@unisql.UUCP> wrat@unisql.UUCP (wharfie) writes:
  13. >In article <C0JvG9.1HBs@austin.ibm.com> dcm@codesmith.austin.ibm.com (Craig Miller - dcm@austin.ibm.com) writes:
  14. >>those who support malloc(0):    ptr = malloc(length);
  15. >>those who don't:        ptr = (length == 0) ? NULL : malloc(length);
  16. >>
  17. >>Right?  And the second case *always* works, regardless of whether the
  18. >>local implementation of malloc supports malloc(0) or not.  Right?
  19. >
  20. >    Nope.  Most code that expects malloc(0) to return a pointer to
  21. >0 bytes does something like
  22. >
  23. >    if (!(ptr=malloc(foo))) goto NO_MEMORY;
  24.  
  25.     My example still works.  Always.  However, I think I understand
  26.     what you're saying.  It should be something like:
  27.  
  28.         if (length == 0)
  29.             ptr = NULL;
  30.         else if ((ptr = malloc(length)) == NULL)
  31.             goto NO_MEMORY;
  32.  
  33.     Still, code that depends on any particular behavior of malloc(0)
  34.     is unportable.  Period.
  35.  
  36.         Craig
  37. -- 
  38. Craig Miller, software mechanic:  AIXV3 Change Team (Level3 support)
  39. #include <ibm/stds/disclaimer.h>
  40. char *internet = "dcm@austin.ibm.com", *vnet = "AUSVM6(TKG007)";
  41.