Brad Aagaard <[email protected]> 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.
pgpr4LfuqOmyX.pgp
Description: PGP signature
