I just saw this code:
artist = (char *) malloc(0);
...and I was wondering why would one do this?
malloc(0) will return NULL or a valid pointer which can be rightly passed to free. And though it seems like the memory that it points to is useless or it can't be written to or read from, that is not always true. :)
int *i = malloc(0);
*i = 100;
printf("%d", *i);
We expect a segmentation fault here, but surprisingly, this prints 100! It is because malloc actually asks for a huge chunk of memory when we call malloc for the first time. Every call to malloc after that, uses memory from that big chunk. Only after that huge chunk is over, new memory is asked for.
Use of malloc(0): if you are in a situation where you want subsequent malloc calls to be faster, calling malloc(0) should do it for you (except for edge cases).
According to Reed Copsey answer and the man page of malloc , I wrote some examples to test. And I found out malloc(0) will always give it a unique value. See my example :
char *ptr;
if( (ptr = (char *) malloc(0)) == NULL )
puts("Got a null pointer");
else
puts("Got a valid pointer");
The output will be "Got a valid pointer", which means ptr
is not 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, andfree(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
.
Just to correct a false impression here:
artist = (char *) malloc(0);
will never ever return NULL
; it's not the same as artist = NULL;
. Write a simple program and compare artist
with NULL
. if (artist == NULL)
is false and if (artist)
is true.
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.
Not sure, according to some random malloc source code I found, an input of 0 results in a return value of NULL. So it's a crazy way of setting the artist pointer to NULL.
http://www.raspberryginger.com/jbailey/minix/html/lib_2ansi_2malloc_8c-source.html