...
A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined.
If the misaligned pointer is dereferenced, the program may terminate abnormally. On some architectures, the cast alone may cause a loss of information even if the value is not dereferenced if the types involved have differing alignment requirements.
...
EXP36-C-EX2: If a pointer is known to be correctly aligned to the target type, then a cast to that type is permitted. There are several cases where a pointer is known to be correctly aligned to the target type. The pointer could point to an object declared with a suitable alignment specifier. It could point to an object returned by aligned_alloc()
, calloc()
, malloc()
, or realloc()
, as per the C standard, section 7.2224.3, paragraph 1 1 [ISO/IEC 9899:20112024].
This compliant solution uses the alignment specifier, which is new to C11, to declare the char
object c
with the same alignment as that of an object of type int
. As a result, the two pointers reference equally aligned pointer types:
...