The context is: One would like to get a fairly well aligned memory, e.g. for allowing the compiler to use AVX, AVX2 . At the same time, one attempts to keep the code as portable as reasonably possible. Allocating using aligned_alloc
sound nice, but C11
lists this as undefined behaviour:
"The alignment requested of the aligned_alloc function is not valid or not supported by the implementation, or the size requested is not an integral multiple of the alignment"
Thus, using for example aligned_alloc(32, 128)
can have great results often, but the other day, it can be UB in some other compiler.
For that case, less strict alignment would suffice, resulting in slower, but correct code. Thus some ideal approach would request strictest alignment available up to 128 in this case. Is there any way to achieve something like this?
-- The question is mainly about using aligned_alloc
, instead up creating one's own allocator.
==EDIT==
Already answered in comment, it is _Alignof (max_align_t)
thx chux , I overlooked that one!