You will not be surprised to hear that posix_memalign()
is standardized as part of POSIX, not part of standard C. As such, providing that function is not a requirement on conforming C implementations. On the other hand, as part of POSIX, it is widely available.
malloc()
promises to return a pointer to memory aligned properly for an object of any type. I'm not sure why you want to ensure an even stronger alignment requirement, but your next best bet for doing so is the aligned_alloc()
function, which is standard C since C2011. If your C library conforms to C2011, then you can replace your posix_memalign()
call with
#include <stdlib.h>
#include <errno.h>
// ...
new = aligned_alloc(32, num);
int ret = (new ? 0 : errno);
If you don't have aligned_alloc()
, either, then your implementation may provide other alternatives, but none of them are standard.