2 ms·
Others have explained pretty well about UBs, but I would give an explicit evidence against 2 by quoting the actual glibc code [1]: /* Allocate SIZE bytes o
by lifthrasiir 3y ago
Others have explained pretty well about UBs, but I would give an explicit evidence against 2 by quoting the actual glibc code [1]:
/* Allocate SIZE bytes of memory. */
extern void *malloc (size_t __size) __THROW __attribute_malloc__
__attribute_alloc_size__ ((1)) __wur;
Expanding macro gives three GCC function attributes [2]: `__attribute__ ((malloc))`, `__attribute__ ((alloc_size(1)))` and `__attribute__ ((warn_unused_result))`. They are used for GCC (and others recognizing them) to actually ensure that they behave as the standard dictates and give better error messages. Your own malloc-like functions won't be treated same unless you give similar attributes.
[1] https://github.com/bminor/glibc/blob/807690610916df8aef17cd14bfadd5d4b6e699a9/stdlib/stdlib.h#L671-L676 https://github.com/bminor/glibc/blob/807690610916df8aef17cd1...
[2] https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attribute...