3 ms·
Why should it be allowed to return a valid pointers anyways? Surely it should always return NULL?
by AaronDinesh 1y ago
Why should it be allowed to return a valid pointers anyways? Surely it should always return NULL?
- Joker_vD 1y agoFor instance, because you are prohibited from passing NULL to e.g. memcpy and lots of other library functions from memory.h/string.h, even when you explicitly specify a size of 0. Another use was to use it to mint unique cookies/addresses, but malloc(1) works for this just as well.
- TZubiri 1y agoMmmmh, cookies
- snickerbockers 1y agoIt's not a valid pointer because you can't use the indirection operator on it. Returning a value other than NULL makes sense because an allocation of size zero is still an allocation. Additionally the actual amount of memory malloc allocates is implementation-defined so long as it is not less than the amount requested, but accessing this extra memory is undefined behavior since processes don't know if it exists or not. a non-NULL return could be interpreted as malloc(0) allocating more than zero bytes. Some implementations don't actually perform the allocation until theres a pagefault from the process writing to or reading from that memory so in that sense a non-NULL return is valid too. I'd argue that malloc(0)==NULL makes less sense because there's no distinction between failure and success. The only real problem is specifying two alternate behaviors and declaring them both to be equally valid.
- cjensen 1y agoThere are three reasonable choices: (a) return the null pointer (b) return a valid unique pointer and (c) abort(). The point of the original C Standard was to make rules about these things AND not break existing implementations. They recognized that (a) and (b) were in existing implementations and were reasonable, and they chose not to break the existing implementations when writing the standard. This is similar to the extremely unfortunate definition of the NULL macro. There were two existing styles of implementation (bare literal 0 and (void *) 0) and the Standard allows either style. Which means the NULL macro is not entirely safe to use in portable code.
- commandlinefan 1y ago> return a valid unique pointer A pointer to what, though? If the requester asked for 0 bytes of memory, you'd either be pointing to memory allocated for another purpose (!) or allocating a few bytes that weren't asked for. > This makes people unhappy for various reasons I read through all the links trying to figure out what those reasons might be and came up empty, I'm still curious why anybody would expect or rely on anything except a null pointer in this instance.
- tedunangst 1y agoYou can copy from a zero sized pointer with memcpy, but not NULL.
- ncruces 1y agoThat's about to change: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf
- DSMan195276 1y ago> allocating a few bytes that weren't asked for. FWIW the alignment guarantees of `malloc()` mean it often will have to allocate more than you ask for (before C23 anyway). You can't 'legally' use this space, but `malloc()` also can't repurpose it for other allocations because it's not suitably aligned. That said I still agree it's a hack compared to just using `malloc(1)` for this purpose, it's well-defined and functionally equivalent if you're looking for a unique address. The fact that you don't know what `malloc(0)` is going to do makes it pretty useless anyway.
- Joker_vD 1y ago> before C23 anyway Did they change "suitably aligned for any object type" to "suitably aligned for any object type with size less than or equal to what was requested" or something like in C23?
- mcherm 1y agoThe behavior of malloc(x) for any positive value x is to either return NULL (meaning that the system was unable to provide a new chunk of memory to use) OR to return a unique pointer to X bytes of data which the program can use. By extension, if x == 0, doesn't it make sense for the system to either return NULL OR to return a pointer to 0 bytes of memory which the program can use? So the standard promises exactly that: to return either NULL or else a unique pointer where that the program has permission to use zero bytes starting at that pointer.
- deleted 1y ago[deleted]
- bobmcnamara 1y ago> Why should it be allowed to return a valid pointers anyways? malloc(0) is allowed to return non-NULL because the standard decrees it. One way of thinking is that all mallocated pointers must always be freed exactly once. Then you're portable.