tags:

views:

542

answers:

6
+6  Q: 

zero size malloc

Very simple question, I made the following program :

#include <stdlib.h>
int main(int argc, char ** argv)
{
    void * ptr;
    ptr = malloc(0);
    free(ptr);
}

And it does not segfault on my machine. Is it a portable behaviour of stdlib malloc and free, or am I looking for trouble ?

Edit : What seems non portable is the value returned by malloc. The question is about the malloc(0) + free combination, not the value of ptr.

+6  A: 

It's allowed to return NULL, and it's allowed to return a non-NULL pointer you can't dereference. Both ways are sanctioned by the standard (7.20.3):

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.

dfa
This is not exactly the question asked. I don't care about the value returned by malloc, but about calling free on a pointer returned by malloc(0).
shodanex
It's still a very good answer because together with Key's answer it gives a complete overview over the behaviour.
schnaader
+1  A: 

Sorry for the trouble, I should have read the man pages :

malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

It seems it is true at least for the gnu libc

shodanex
RTFM :) http://en.wikipedia.org/wiki/RTFM
Nick D
+1  A: 

Updated taking into account libt & Pax's comments:

The behaviour of calling malloc(0) is implementation dependant or in other words non-portable and undefined.

Link to CFaq question for more detail.

Aditya Sehgal
The ISO standards makes a very clear distinction between defined, implementation-defined and undefined. You should too. Implementation-defined means it *is* defined but the doco for that implementation will tell you what it does. Undefined means it can do *absolutely anything* including, but not limited to, total destruction of the universe.
paxdiablo
thanks for the clarification (and the down vote ;-) ). I used the term "undefined" rather loosely there. My bad.
Aditya Sehgal
You misunderstand the faq. it doesn't mean to say the overall behavior is implementation defined. It means that whether or not the result is NULL or some other value is implementation defined. For example, valid behavior does not include sending a sigsegv.
Johannes Schaub - litb
@aditya, the comment was mine, the downvote was not - I usually give people a chance to edit their answer before downvoting.
paxdiablo
@aditya, hold on. I didn't downvote you either :)
Johannes Schaub - litb
There ya go, have a +1 for changing it :-)
paxdiablo
great to see this answer changed, +1
Johannes Schaub - litb
A: 

According to the c standard

7.20.3 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.

rohittt
+17  A: 

The behaviour is implementation defined, you will receive either a NULL pointer or an address. Calling free for the received pointer should however not cause a problem since:

  • free(NULL) is ok, no operation is done
  • free(address) is ok, if address was received from malloc (or others like calloc etc.)
Key
+1  A: 

Though it might be legal C/C++, it is indicative a bigger problems. I generally call it 'pointer slopiness'.

See "Do not make assumptions about the result of malloc(0) or calloc(0)", https://www.securecoding.cert.org/confluence/display/seccode/VOID+MEMxx-A.+Do+not+make+assumptions+about+the+result+of+malloc%280%29+or+calloc%280%29.

Jeffrey Walton
It is not about the result of malloc(0) but about, the malloc(0) + free combination
shodanex