Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine maximum required alignment in C99

Is there a portable way that only relies on what the C99 standard provides to find out the maximum required alignment that is needed for any data type.

Like maxalign_t in C++11.

What I'm currently doing is calculating the least common multiple (lcm) of the alignments of int, long int, long long int, double, void * and size_t as best effort way of determining the alignment.

Update: I currently need this for implementing a wrapper around malloc that stores metadata at the beginning of the block of memory and returns a pointer with a higher address than what malloc has returned.

like image 598
FSMaxB Avatar asked Jul 08 '16 16:07

FSMaxB


1 Answers

There isn't really a good way to do that, which is why maxalign_t was introduced by C11. Though, I cannot imagine an ordinary system where a type with higher alignment requirements than intmax_t exists so you might as well use that and get the right answer for 99% of the systems when maxalign_t is not available.

like image 77
fuz Avatar answered Oct 12 '22 13:10

fuz