Some bits on malloc(0) in C being allowed to return NULL

原始链接: https://utcc.utoronto.ca/~cks/space/blog/programming/CZeroSizeMallocSomeNotes

Forbidden You don't have permission to access /~cks/space/blog/programming/CZeroSizeMallocSomeNotes on this server.

Here's a summary of the Hacker News thread: The discussion revolves around the behavior of `malloc(0)` in C, specifically whether it should be allowed to return NULL or a valid pointer. The original article linked discusses this topic. One commenter described a past system where `malloc(0)` returned -1 and incremented a counter, with `free(-1)` decrementing it, as a method to detect memory leaks (specifically zero-sized allocations). However, others pointed out that this is non-compliant as the standard specifies that `malloc(0)` should return a unique pointer if not NULL. There was debate on how this helps for leak detection, and whether returning NULL would be a better approach. Several reasons for allowing a valid pointer are discussed, including avoiding NULL pointers as input to functions like `memcpy` even when the size is 0, and creating unique cookies/addresses.
相关文章

原文

Forbidden

You don't have permission to access /~cks/space/blog/programming/CZeroSizeMallocSomeNotes on this server.

联系我们 contact @ memedata.com