You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "xiaoxiang781216 (via GitHub)" <gi...@apache.org> on 2023/05/01 06:10:44 UTC

[GitHub] [nuttx] xiaoxiang781216 commented on pull request #9143: mm: malloc return valid pointer when request 0 size

xiaoxiang781216 commented on PR #9143:
URL: https://github.com/apache/nuttx/pull/9143#issuecomment-1529390428

   Most popular implementation list in https://en.wikipedia.org/wiki/C_standard_library returns non-NULL pointer for malloc(0):
   https://github.com/bminor/glibc/blob/master/malloc/malloc.c#L115-L116
   https://github.com/huangqinjin/ucrt/blob/master/heap/malloc_base.cpp#L29
   https://github.com/bminor/musl/blob/master/src/malloc/lite_malloc.c#L50
   https://github.com/bminor/newlib/blob/master/newlib/libc/stdlib/_mallocr.c#L1420-L1423
   https://github.com/picolibc/picolibc/blob/main/newlib/libc/stdlib/mallocr.c#L109-L110
   https://github.com/jemalloc/jemalloc/blob/dev/src/jemalloc.c#L2305-L2329
   
   Two give user compile time choice:
   https://github.com/ensc/dietlibc/blob/master/lib/alloc.c#L155-L159
   https://git.uclibc.org/uClibc/tree/libc/stdlib/malloc/malloc.c#n211
   
   Only one return NULL:
   https://kernel.googlesource.com/pub/scm/libs/klibc/klibc.git/+/refs/heads/master/usr/klibc/malloc.c
   
   So, many applications may implicitly depend on that malloc(0) return non-NULL pointer without caution. 
   My question is that:
   
   1. The standard explicitly states that it's a legal implementation that malloc(0) return non-NULL pointer
   2. Most popular libc implementation return non-NULL for malloc(0)
   3. There is no bad side effect to return non-NULL pointer for malloc(0)
   4. Many applications depends on this behavior
   
   What's the benefit to return NULL pointer when user call malloc with zero?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org