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

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!mdisea!mitchell
  3. From: mitchell@mdd.comm.mot.com (Bill Mitchell)
  4. Subject: Re: malloc(0)
  5. Message-ID: <1992Nov21.191712.1397@mdd.comm.mot.com>
  6. Sender: news@mdd.comm.mot.com
  7. Bcc:  mitchell
  8. Reply-To: mitchell@mdd.comm.mot.com (Bill Mitchell)
  9. Organization: Motorola, Mobile Data Division - Seattle, WA
  10. References: <1ejhrnINN5it@ftp.UU.NET> <1992Nov21.005207.9741@lucid.com> <3904@dozo.and.nl>
  11. Date: Sat, 21 Nov 1992 19:17:12 GMT
  12. Lines: 81
  13.  
  14. in comp.std.c, jos@and.nl (Jos Horsmeier) said:
  15.  
  16.  
  17. >In article <1992Nov21.005207.9741@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
  18. >|The standard says  (refering to allocation routines)
  19. >|
  20. >|    If the size of the space requested is zero, the behavior
  21. >|    is implementation-defined; the value returned shall be
  22. >|    either a null pointer or a unique pointer.
  23. >|
  24. >|I have always read "unique pointer" as meaning different from
  25. >|previously allocated pointers including pointers previously
  26. >|returned by malloc(0).  However I have just been talking to
  27. >|some people who insist it means that the same pointer is returned
  28. >|on each call of malloc(0).
  29. >|
  30. >|Comments?
  31. >
  32. >Quite an interesting question ... the answer all depends on the meaning
  33. >of the word `unique'. Consider the following snippet of code:
  34. >
  35. >char *p, *q;
  36. >
  37. >p= malloc(n);
  38. >q= malloc(n);
  39. >
  40. >if (p == q) /* etc */
  41. >
  42. >The condition always fails if malloc succeeds. If malloc(0) returns
  43. >a non NULL value (i.e. it doesn't fail) it should return a unique
  44. >pointer value every time it is invoked as malloc(0). On the other
  45. >hand, suppose malloc(0) returns NULL. The test would (obviously)
  46. >succeed. Because we don't want to break the code, when porting it
  47. >from a non-NULL returning implementation to a NULL returning imple-
  48. >mentation, this tends me to believe that malloc(0) should return the
  49. >same value over and over again, but that value must be different
  50. >from any other malloc(n) call, where n is non zero. But on the 
  51. >other hand, this would violate the constraint that the pointer
  52. >values `shall be unique.' The ISO standard doesn't have a rationale
  53. >included, so I can't say much more about this ...
  54. >
  55.  
  56. Quoted from the rationale for the ansi standard:
  57.  
  58.   4.10.3 Memory management functions
  59.  
  60.   The treatment of null pointers and 0-length allocation requests in the
  61.   definition of these functions was in part guided by a desire to support
  62.   this paradigm:
  63.   
  64.       OBJ * p; /* pointer to a variable list of OBJ's */
  65.   
  66.       /* initial allocation */
  67.       p = (OBJ *) calloc(0, sizeof(OBJ));
  68.       /* ... */
  69.   
  70.       /* reallocations until size settles */
  71.       while(/* list changes size to c */) {
  72.           p = (OBJ *) realloc((VOID *)p, c * sizeof(OBJ));
  73.           /* ... */
  74.       }
  75.   
  76.   This coding style, not necessarily endorsed by the Comittee, is reported
  77.   to be in widespread use.
  78.   
  79.   Some implementations have returned non-null values for allocation requests
  80.   of 0 bytes.  Although this strategy has the theoretical advantage of
  81.   distinguishing between "nothing" and "zero" (an unallocated pointer vs
  82.   a pointer to zero-length space), it has the more compelling theoretical
  83.   disadvantage of requiring the concept of a zero-length object.  Since
  84.   such objects cannot be declared, the only way they could come into
  85.   existance would be through such allocation requests.  The Committee
  86.   has decided not to accept the idea of zero-length objects.  The
  87.   allocation functions may therefore return a null pointer for an
  88.   allocation request of zero bytes.  Note that this treatment does not
  89.   preclude the paradigm outlined above.
  90.   
  91.   
  92. -- 
  93. mitchell@mdd.comm.mot.com (Bill Mitchell)
  94.  
  95.