malloc(0) returning 0 is expected behaviour on AIX but compiling with -D_LINUX_SOURCE_COMPAT, malloc(0) should return a valid pointer. As per: https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.basetrf1/malloc.html It doesn't work for c++ program which includes <cstdlib>. In the header it states: // Get rid of those macros defined in <stdlib.h> in lieu of real functions. ... #undef malloc The above seems to be causing the issue and resetting the behaviour of -D_LINUX_SOURCE_COMPAT. Code to reproduce: >cat malloc.cpp #include <stdio.h> //#include <stdlib.h> //<-- Works this stdlib #include <cstdlib> int main() { printf("%p \n", malloc(0)); return 0; } >g++ malloc.cpp -D_LINUX_SOURCE_COMPAT >./a.out 0
I can reproduce this on AIX 7.1.3.0 but it returns a valid pointer on AIX 7.2.0.0
N.B. with GCC 6 and later you get the same behaviour for <stdlib.h> because that includes <cstdlib> now. My tests on AIX 7.1.3.0 were not the same GCC version, it looks like this was fixed for GCC 6.2.0 by r233029 (and r237394 on trunk). The fixed header now has: # 748 "/home/jwakely/gcc/6/lib/gcc/powerpc-ibm-aix7.2.0.0/6.3.1/include-fixed/stdlib.h" 3 4 extern void *__linux_malloc(size_t); extern void *__linux_realloc(void *, size_t); extern void *__linux_calloc(size_t, size_t); extern void *__linux_valloc(size_t); extern void *malloc(size_t) __asm__("__linux_malloc"); extern void *calloc(size_t, size_t) __asm__("__linux_calloc"); extern void *realloc(void *, size_t) __asm__("__linux_realloc"); extern void *valloc(size_t) __asm__("__linux_valloc"); This is necessary because the C++ standard explicitly forbids malloc and other functions from the C library from being defined as macros, they must be defined as functions.
It was also fixed on the gcc-5-branch by r237479