[petsc-dev] PetscMalloc for size zero

Brad Aagaard baagaard at usgs.gov
Thu Jan 30 12:46:08 CST 2014

On 01/30/2014 10:34 AM, Jed Brown wrote:
> Brad Aagaard <baagaard at usgs.gov> writes:
>> Matt and Jed,
>> I see that Jed pushed some changes (jed/malloc-zero) for PetscMalloc to
>> deal with memory alignment and a zero size. It looks like the pointer
>> will NOT be NULL for a size of 0. Is this true?
> Yes, just like malloc(), it can be either a unique pointer or NULL.  You
> need the size anyway to know how many elements are in the array.

I thought it was a nice feature that PETSc improved on malloc() and 
free() by returning NULL for zero sized allocation (although this wasn't 
true for --with-debugging=0 due to memory alignment) and set pointers to 
NULL after freeing.

What is the rationale for not returning NULL for mallocs of size zero 
other than conforming to C malloc behavior?


More information about the petsc-dev mailing list