[petsc-dev] PetscMalloc for size zero
Jed Brown
jed at jedbrown.org
Thu Jan 30 13:05:17 CST 2014
Brad Aagaard <baagaard at usgs.gov> writes:
> 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?
The standard could have easily specified that malloc(0) returns NULL.
It didn't because leaving it unspecified allows better memory-checking
tools (e.g., you can check for matching free even when your tests happen
to allocate 0 bytes). As you mention, PETSc did not guarantee it either
--with-debugging=0, so correct code cannot depend on it. We are just
now being explicit that we only make the same guarantees as malloc().
We still set pointers to NULL after freeing.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20140130/d4d1a9dd/attachment.sig>
More information about the petsc-dev
mailing list