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