[LLVMdev] malloc(), free(), and alloca() with zero size

Dale Johannesen dalej at apple.com
Sat Oct 6 09:35:03 PDT 2007


On Oct 6, 2007, at 9:14 AM, Jon Sargeant wrote:

> If <NumElements> is zero, what is the behavior of malloc() and  
> alloca()?
> Can I call free() using the pointer that malloc() returns?

alloca is not standard.
The behavior of malloc is covered in 7.20.3p1:

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.

Portable code can't depend on either behavior, obviously.  It is  
usually simplest to avoid using malloc(0).

> Also, I'm assuming that free()ing a null pointer is a legal NOP?

Yes, 7.20.3.2p2.




More information about the llvm-dev mailing list