What's the point of malloc(0)?
I just saw this code:
artist = (char *) malloc(0);
...and I was wondering why would one do this?
According to the specifications, malloc(0) will return either "a null pointer or a unique pointer that can be successfully passed to free()".
This basically lets you allocate nothing, but still pass the "artist" variable to a call to free() without worry. For practical purposes, it's pretty much the same as doing:
artist = NULL;
The C standard (C17 7.22.3/1) says:
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.
So, malloc(0)
could return NULL
or a valid pointer that may not be dereferenced. In either case, it's perfectly valid to call free()
on it.
I don't really think malloc(0)
has much use, except in cases when malloc(n)
is called in a loop for example, and n
might be zero.
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.
So, he made sure that artist
and other variables always had some "valid" value in them. The comment says as much: // these must always point at malloc'd data
.
malloc(0) behaviour is implementation specific. The library can return NULL or have the regular malloc behaviour, with no memory allocated. Whatever it does, it must be documented somewhere.
Usually, it returns a pointer that is valid and unique but should NOT be dereferenced. Also note that it CAN consume memory even though it did not actually allocate anything.
It is possible to realloc a non null malloc(0) pointer.
Having a malloc(0) verbatim is not much use though. It's mostly used when a dynamic allocation is zero byte and you didn't care to validate it.
There's an answer elsewhere on this page that begins "malloc(0) will return a valid memory address and whose range will depend on the type of pointer which is being allocated memory". This statement is incorrect (I don't have enough reputation to comment on that answer directly, so can't put this comment directly under there).
Doing malloc(0) will not automatically allocate memory of correct size. The malloc function is unaware of what you're casting its result to. The malloc function relies purely on the size number that you give as its argument. You need to do malloc(sizeof(int)) to get enough storage to hold an int, for example, not 0.
There are a lot of half true answers around here, so here are the hard facts. 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().
That means, there is absolutely no guarantee that the result of malloc(0)
is either unique or not NULL. 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.
So, whatever malloc(0)
returns, it can safely be passed to free()
. But so can a NULL
pointer.
Consequently, writing artist = malloc(0);
is in no way better than writing artist = NULL;