Possible Duplicates:
what does malloc(0) return ?
what’s the point in malloc(0)?
Why does malloc(0) return valid memory address ? What's the use ?
Possible Duplicates:
what does malloc(0) return ?
what’s the point in malloc(0)?
Why does malloc(0) return valid memory address ? What's the use ?
If the size of the space requested is 0, the behavior is implementation-defined: the value returned shall be either a null pointer or a unique pointer.
Source: malloc
at The Open Group
I guess the address is valid only if you want to use 0 bytes from it. The use would probably be not having to specially treat cases like:
char * foo = malloc(size);
// do something with foo
free(foo);
It does not return a valid address.
The result of malloc(0) is implementation defined and therefore unreliable.
Some compiler implementations will cause it to return a NULL pointer, others may return some other value (but it still can't/shouldn't be used to access memory).
The memory cannot be used, (however it may require free()ing).
It isn't mandated in C (but it is mandated for new in C++). There are cases where objects have no state (and so have a null size) but their identity is important. In such case having an allocator which returns valid and different objects for a size of 0 is needed if you need to allocate them dynamically.
I cannot imagine a use for that, but, it is probably the result of malloc's design: if it's easier to support it than to not support, it should be supported.
Just as it's okay to take asin(4), and it returns a complex number... oops no it doesn't...