home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / std / c / 3048 < prev    next >
Encoding:
Text File  |  1992-11-21  |  1.9 KB  |  52 lines

  1. Path: sparky!uunet!mcsun!sun4nl!and!jos
  2. From: jos@and.nl (Jos Horsmeier)
  3. Newsgroups: comp.std.c
  4. Subject: Re: malloc(0)
  5. Message-ID: <3904@dozo.and.nl>
  6. Date: 21 Nov 92 16:20:34 GMT
  7. References: <1ejhrnINN5it@ftp.UU.NET> <1992Nov21.005207.9741@lucid.com>
  8. Organization: AND Software BV Rotterdam
  9. Lines: 41
  10.  
  11. In article <1992Nov21.005207.9741@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
  12. |The standard says  (refering to allocation routines)
  13. |
  14. |    If the size of the space requested is zero, the behavior
  15. |    is implementation-defined; the value returned shall be
  16. |    either a null pointer or a unique pointer.
  17. |
  18. |I have always read "unique pointer" as meaning different from
  19. |previously allocated pointers including pointers previously
  20. |returned by malloc(0).  However I have just been talking to
  21. |some people who insist it means that the same pointer is returned
  22. |on each call of malloc(0).
  23. |
  24. |Comments?
  25.  
  26. Quite an interesting question ... the answer all depends on the meaning
  27. of the word `unique'. Consider the following snippet of code:
  28.  
  29. char *p, *q;
  30.  
  31. p= malloc(n);
  32. q= malloc(n);
  33.  
  34. if (p == q) /* etc */
  35.  
  36. The condition always fails if malloc succeeds. If malloc(0) returns
  37. a non NULL value (i.e. it doesn't fail) it should return a unique
  38. pointer value every time it is invoked as malloc(0). On the other
  39. hand, suppose malloc(0) returns NULL. The test would (obviously)
  40. succeed. Because we don't want to break the code, when porting it
  41. from a non-NULL returning implementation to a NULL returning imple-
  42. mentation, this tends me to believe that malloc(0) should return the
  43. same value over and over again, but that value must be different
  44. from any other malloc(n) call, where n is non zero. But on the 
  45. other hand, this would violate the constraint that the pointer
  46. values `shall be unique.' The ISO standard doesn't have a rationale
  47. included, so I can't say much more about this ...
  48.  
  49. kind regards,
  50.  
  51. Jos aka jos@and.nl
  52.