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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With