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.
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.
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.
In Windows:
void *p = malloc(0);
will allocate a zero-length buffer on the local heap. The pointer returned is a valid heap pointer.malloc
ultimately callsHeapAlloc
using the default C runtime heap which then callsRtlAllocateHeap
, etc.free(p);
usesHeapFree
to free the 0-length buffer on the heap. Not freeing it would result in a memory leak.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 :
The output will be "Got a valid pointer", which means
ptr
is not null.Just to correct a false impression here:
artist = (char *) malloc(0);
will never ever returnNULL
; it's not the same asartist = NULL;
. Write a simple program and compareartist
withNULL
.if (artist == NULL)
is false andif (artist)
is true.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