The man-page for malloc says: If size is 0, then malloc returns either NULL, or a unique pointer value that can later be successfully passed to free. The only guarantee is provided by the definition of free , again, here is what the man-page says: If ptr is NULL, no operation is performed. Too bad the implementation isn't allowed to return a non-null, non-unique pointer. That way, malloc 0 could return, say, 0x1, and free could have a special-case check of 0x1 just as it has for 0x0.
Todd Lehman An implementation may do as you suggest. There is no unique requirement. OTOH, returning a non-unique special value may disrupt code that counts on unique values. Perhaps a corner case question for SO. In this case it doesn't, but it still isn't a canonical source for general C. Lundin True. Along with information of which parts adhere to which standard, should they differ. I have the feeling that they both want to be precise, and advertise every single bit that's a GNU extension Steve Jessop Steve Jessop k 34 34 gold badges silver badges bronze badges.
Why you shouldn't do this Shipof 2 2 silver badges 11 11 bronze badges. The link has been moved: wiki. In conjunction to Reed's answer, I would like to point out that there is a similar thing, that appears like an overloaded function realloc : foo is non-NULL and size is zero, realloc foo, size ;.
To actually answer the question made: there is no reason to do that. Paolo Paolo The pointer returned is a valid heap pointer. Not freeing it would result in a memory leak.
Jamal 7 7 gold badges 22 22 silver badges 31 31 bronze badges. Scott Franco Scott Franco 1 1 silver badge 11 11 bronze badges. Doug T. Sagar Bhosale Sagar Bhosale 2 2 silver badges 4 4 bronze badges. Watch out for nasal demons! That is true.
It is implementation specific. I have not tried it on other platforms. Having said that, the concept that I have described has been described in the book "The C programming language" by Brain Kernighan and Dennis Ritchie. I know: super late comment on this question. But there is sometimes a use for malloc 0 that isn't mentioned.
This allows you to get a feel for actual memory usage if you get this before and after a series of allocations. Could you please say which chapter are you referring too?
Providing an exact quote would be nice too. Here is the analysis after running with valgrind memory check tool. Shubhesh Swain Shubhesh Swain 1 1 1 bronze badge. Community Bot 1 1 1 silver badge. Neal Neal 84 3 3 bronze badges. Just because you always got a valid pointer does not mean that it is guaranteed. Chris k 63 63 gold badges silver badges bronze badges.
Suryakant Tiwari Suryakant Tiwari 1 1 1 bronze badge. Krellan is right in pointing out that this answer is wrong: malloc does not know anything about the cast which is actually entirely superfluent in C. Dereferencing the return value of malloc 0 will invoke undefined behavior. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Linked A conforming implementation may return the same address as the one already in ptr.
So, your if condition may return true. Note, however, looking at the value of ptr after realloc ptr, may be undefined behavior. Then, they are guaranteed to be different. Also, the return value from malloc on the LHS hasn't been free d yet, so any other malloc , calloc , or realloc may not return that value. This means that if you wrote your condition as:. On OS X, my code didn't output anything when I ran it.
On Linux, it prints possible, OK. The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated until the space is explicitly deallocated. The lifetime of an allocated object extends from the allocation until the deallocation. Each such allocation shall yield a pointer to an object disjoint from any other object.
The pointer returned points to the start lowest byte address of the allocated space. If the space cannot be allocated, a null pointer is returned. Any program that depends on malloc never returning the same pointer twice, especially with intervening calls to free , is broken. Um, C99 7. Yes, I am. Multiple calls to malloc with non-zero arguments, assuming they all succeed, must return distinct pointers, assuming there are no intervening calls to free.
If malloc 0 returns a non-null result, then it must behave as if it were called with a non-zero argument; it seems to me that means returning distinct values. On the other hand, a portable application can't depend on malloc 0 returning a non-null result anyway, so the guarantee doesn't do much good. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item.
Always check the return from malloc, even if the amount of memory requested is small. The last sentence in the above quote is meant for your own good, and has nothing to do with how the OS might misbehave though it could.
Thanks Microsoft -- jay. Chris Dollin. If an implementation chose to return the same pointer P for each malloc 0 then realloc would need to know that P was operationally equal to null, and hence in your code above the realloc would just mallocate bytes for your convenience.
Wouldn't it? Richard Bos. But Santa is just a cheap copy of the real, Dutch, Saint Nicholas, and that code bad use of realloc excepted does not have undefined behaviour. Therefore, all pointers returned from malloc 0 must be either null or not equal to any other pointer, including ones obtained from other calls to malloc 0. I don't know if it's your metaphor or the imminent arrival of the holiday or what, but I can't see why your example poses a problem to the tactic in my remark above.
Could you unpack? No reason for that complication. It isn't required by the standard. The following from N is adequate: 7. The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated until the space is explicitly freed or reallocated.
Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points to the start lowest byte address of the allocated space. If the space cannot be allocated, a null pointer is returned.
If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.
The value of a pointer that refers to freed space is indeterminate. You're arguing that the standard says that multiple malloc 0 s shall not re-use the same pointer value, yes? That's fine then. Other posters eg Random seemed to be arguing that there would. Er, I suspect this would break a lot of code I believe you may have misspelled "No, I'm not" up above. Would you mind clarifying your clarification? No it couldn't. Think about pointer comparisons. Then think about what other anomalies could exist.
But one is enough to show it is a bad idea. I'm thinking, but I can't see an anomaly. I'm probably just being dim. It depends on your perspective. It's not "actual memory" to C user code. You asked for a zero-byte object, and you got a zero-byte object, so in theory you've allocated zero bytes.
OTOH, it's certainly "actual memory" to the implementation, since malloc does need to request memory from the OS or wherever to store the header, but that header isn't part of a C object as far as user code can tell.
S -- Stephen Sprunk "God does not play dice. Not so. The standard requires malloced pointers to objects be distinct. After the free p p is no longer a pointer to an object. Google is only a poor interface to usenet. There is no reason to assume your readers can, or ever will, see any previous articles.
Yevgen Muntyan. CBFalconer wrote: Random wrote But p2 could also be changed. Your example with realloc assumes implementation returns some sensible pointer on malloc 0 , which can actually point to some memory allocated later, but it's not necessary. I believe Keith Thompson's quote is what really matters here, about non-NULL result of malloc 0 behaving the same as malloc 10 in particular, non-NULL pointers must be different , so all this stuff doesn't matter in practice though : Best regards, Yevgen.
In what sense is the non-null result of malloc 0 a pointer to an object anyway? You snipped one that I showed earlier. Chris Torek. Since malloc is a reserved function name, let me illustrate with a different function-name. This also lets me use malloc for the "hard parts". Then I believe nalloc satisfies the requirements for malloc. Old Wolf.
0コメント