What happens if you malloc 0




















I don't know how Linux system calls would react to such a pointer. Probably unhappily. Stephen Sprunk. A common implementation of malloc writes a header containing links between blocks, size of the user object, and possibly a magic value to detect corruption regardless of allocation size.

In the case of malloc 0 , the header is immediately followed by the next header or a trailer with a magic value to detect corruption, then the next header. Therefore, successive calls will return different addresses, since each new header starts at a different address. Do it enough times and you can run out of heap space even though you're theoretically allocating no memory : S -- Stephen Sprunk "God does not play dice. Do it enough times and you can run out of heap space even though you're theoretically allocating no memory : So the memory allocated for the header isn't "actual memory"?

Kohn Emil Dan. It's not really clear why they'd have to be different. A quick test of a few systems I happen to have immediate access to shows that all of them either return a null pointer, or return distinct values on two successive calls to malloc 0. As for why this is required, C99 7. One aspect of the behavior of malloc with a non-zero size is that successive calls return unique values. My interpretation is that this same behavior is required for non-null results of malloc 0.

The simplest way to implement this is to quietly translate "malloc 0 " to "malloc 1 ". They'd better not do that C99 7.

But the question is whether the kernel pays attention to the pointer value if the associated length is 0. It might, or it might not, or it might differ from one system call to another. Simon Biber. Return a null pointer or a pointer to 0 bytes? Please don't top-post. We prefer inline replying here.

Bill Medland. Well, I expect it frequently does allocate memory; it presumably just allocates enough for its own management purposes rather than what it needs and what the caller wants and the text is the usual user-intended Microsoft text rather than the pedantic version -- Bill Medland. Well, I expect it frequently does allocate memory; it presumably just allocates enough for its own management purposes rather than what it needs and what the caller wants and the text is the usual user-intended Microsoft text rather than the pedantic version Or it allocates a unique and invalid address without allocating any memory at that address.

Note it doesn't say no memory is consumed, just none is allocated. I could say the same about nmalloc. As far as the user is concerned he asked for, and got, zero bytes. Now, if p2 had free space above it, p2 might be unchanged. However p1 is still pointing to zero bytes.

Do you have a standard citation for that? 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 Collectives on Stack Overflow. Learn more. What's the point of malloc 0? Ask Question. Asked 11 years, 10 months ago. Active 10 months ago. Viewed 79k times. Lii Add a comment.

Active Oldest Votes. Reed Copsey Reed Copsey k 72 72 gold badges silver badges bronze badges. As mentioned by C. It could be dangerous to rely on this in cross-platform situations. Really wish the specs would say "safely passed to realloc" as well -. NSAddict "empty struct where sizeof will return 0", please provide an example, sounds like a language extension. Should be enough. Show 6 more comments. The C standard C17 7. Looking at the code in the link, I believe that the author had two misconceptions: malloc 0 returns a valid pointer always , and free 0 is bad.

Lundin k 35 35 gold badges silver badges bronze badges. Alok Singhal Alok Singhal The fact that it is implementation dependent makes it more or less completely useless - this is one of the crappier bits of the C standard, and quite a few of the standards comittee for example P. Plauger have moaned about it. I agree. If malloc 0 returned a valid pointer, then malloc returning NULL means "failure" always, and 0 isn't a special case anymore, which is more consistent.

Can you realloc a pointer returned by malloc 0? Braden Best Yes to both. Show 3 more comments. It is possible to realloc a non null malloc 0 pointer. Coincoin Coincoin 26k 7 7 gold badges 50 50 silver badges 74 74 bronze badges. So, if malloc 0 does not return NULL , it will use memory to store that information, and if not free d, will constitute a memory leak. Malloc implementations perform record keeping which could add a certain amount of data per pointer returned on top of the size requested.

Memory consumed and memory allocated does not mean the same thing. In this very case, most implementation will return a unique pointer. This mean a part of the address space needs to be sacrificed for that pointer. Depending on the allocator, this might actually mean it will allocate 1 byte or more. The library can do whatever it wants - well, it can either return a unique pointer that no other malloc will return, or return NULL.

However, in the very same implementation of the standard C library, realloc ptr, 0 frees ptr and returns NULL. Show 4 more comments. Krellan Krellan 3 3 silver badges 5 5 bronze badges. 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 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. In C89, malloc 0 is implementation dependent - I don't know if C99 has fixed this or not.

Of course, you can't use the pointer to access what it points to without invoking undefined behaviour. As to why this exists, it is convenient for some algorithms, and means you don't need to litter your code with tests for zero values. Python Javascript Linux Cheat sheet Contact. The question is about realloc malloc 0 , 0 : What does malloc 0 return?



0コメント

  • 1000 / 1000