How common are malloc(0) and realloc(p, 0)?
Posted: Tue Jun 17, 2025 1:05 pm
Hi all,
there's an ongoing thread on the musl mailing list right now (plus a couple of others) about malloc(0) and realloc(p, 0), specifically about making them consistent with each other. And while reading that thread I was constantly wondering if I was taking crazy pills.
In all my life, I have seen a lot of C code, I have written a lot of C code, and I have debugged quite a lot of C code. And in that time, never, not once, have I had occasion to use malloc(0) for anything. But suddenly there's this dude really passionately arguing that malloc should be required to attempt to allocate a zero-sized object if called with 0 as argument. And other people I previously respected agree with him! Why in three devils' name should malloc() do such a thing? C doesn't have any zero-sized types, and so cannot have zero-sized objects. Consequently, I made my version of malloc return NULL/EINVAL when someone attempts to allocate 0 bytes.
And realloc(p, 0) I have never understood. What is the point? realloc() can only return NULL or a valid pointer, right? And NULL means an error occurred and the original pointer is still valid. So realloc(p, 0) cannot have the meaning of freeing the pointer, because then p is no longer a valid pointer to return, and it cannot return NULL because that would mean error and no side effects. My version of realloc returns NULL/EINVAL and has no side effects.
So what about you guys? Have you ever seen malloc(0) and realloc(p, 0) honestly used for anything in an application? Or is that thread just an academic ivory tower discussion of the highest order?
there's an ongoing thread on the musl mailing list right now (plus a couple of others) about malloc(0) and realloc(p, 0), specifically about making them consistent with each other. And while reading that thread I was constantly wondering if I was taking crazy pills.
In all my life, I have seen a lot of C code, I have written a lot of C code, and I have debugged quite a lot of C code. And in that time, never, not once, have I had occasion to use malloc(0) for anything. But suddenly there's this dude really passionately arguing that malloc should be required to attempt to allocate a zero-sized object if called with 0 as argument. And other people I previously respected agree with him! Why in three devils' name should malloc() do such a thing? C doesn't have any zero-sized types, and so cannot have zero-sized objects. Consequently, I made my version of malloc return NULL/EINVAL when someone attempts to allocate 0 bytes.
And realloc(p, 0) I have never understood. What is the point? realloc() can only return NULL or a valid pointer, right? And NULL means an error occurred and the original pointer is still valid. So realloc(p, 0) cannot have the meaning of freeing the pointer, because then p is no longer a valid pointer to return, and it cannot return NULL because that would mean error and no side effects. My version of realloc returns NULL/EINVAL and has no side effects.
So what about you guys? Have you ever seen malloc(0) and realloc(p, 0) honestly used for anything in an application? Or is that thread just an academic ivory tower discussion of the highest order?