Search code examples
clinuxalignmentposix

Determining portably the most restrictive data type for alignment


Choosing uintmax_t handles the integer case if I'm not overlooking something.

(1) Is there a similar such data type for floats, and, if yes, in which header? (it's not in float.h for me)

(2) is it correct that choosing a union of these 2 data types (assuming we can answer (1) in the affirmative) should always be most restrictive?


Solution

  • As pointed out in Eric's answer, C11 defines an object type with the greatest fundamental alignment in <stddef.h>: max_align_t.

    Note, however, that this might not still work as expected on GCC. Taken from gcc's website:

    A fourth version of the C standard, known as C11, was published in 2011 as ISO/IEC 9899:2011. GCC has substantially complete support for this standard, enabled with -std=c11 or -std=iso9899:2011. (While in development, drafts of this standard version were referred to as C1X.)

    If you don't want to rely on C11 due to its freshness and current lack of support, I suggest defining a union with all integer types, all floating point types, a void pointer, and a function pointer: one of these must be the most restrictive type.