C Reference ยท Concepts and Tools
Strict aliasing in C
Explain the C strict aliasing rule: which types may access the same memory, why pointer casts break it, and how to reinterpret bytes safely with memcpy or unions.
Two pointers alias when they refer to the same memory. The strict aliasing rule says the compiler may assume that a pointer to one type does not point at an object of an unrelated type. An int * and a float * are assumed to point at different objects, so the compiler can keep a value in a register across a write through the other pointer, reorder loads and stores, and vectorize loops.
The rule is what lets C compile tight loops well. It becomes a trap when code reinterprets memory by casting a pointer: *(float *)&some_int reads an int object through a float lvalue, which is undefined behavior. The program may appear to work at -O0 and fail at -O2.
uint32_t bits = 0x3F800000; float f = *(float *)&bits; /* WRONG: violates strict aliasing */ float g; memcpy(&g, &bits, sizeof g); /* correct: copy the bytes */
The rule
An object may be accessed only through an lvalue of one of these types:
- its own type, or a version of it with extra qualifiers (
const,volatile); - the signed or unsigned variant of its type (
intandunsigned int); - a
structorunionthat contains the type among its members (including recursively); - a character type (
char,signed char,unsigned char), which can inspect the bytes of any object; - a compatible type, for example a different
typedeffor the same type.
Memory obtained from malloc has no declared type; its type is set by the last store, so a malloced block can be used as an int array, then (after being written as another type) as something else, as long as each access matches the stored type. Everything outside these cases is undefined behavior.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Usage notes
memcpyis the portable fix. Modern compilers turn a fixed-sizememcpyinto a single register move, so there is no performance cost. Use it for parsing network data and file formats, and to read afloat's bits.- Unions for punning are explicitly allowed by C (a note in the standard says the bytes are reinterpreted), but C++ forbids them. Prefer
memcpyfor code that must compile as both. - Alignment is a separate rule. Casting
char *data toint *can produce a misaligned pointer, which is undefined even if you never break aliasing.memcpyavoids both problems. - Signed and unsigned versions may alias, so
*(unsigned *)&signed_intis allowed; the value is just reinterpreted. -fno-strict-aliasingdisables the optimization in GCC and Clang, and some projects (the Linux kernel) do. It hides the bug, it does not fix it, and it slows loops down.-Wstrict-aliasing(part of-Wallat-O2) catches only the obvious cases.restrictis the opposite promise: you tell the compiler two pointers of the same type do not overlap. Breaking that promise is also undefined.- Sanitizers do not detect most aliasing bugs. Review pointer casts by hand and keep them out of portable code.
Related
- restrict and union
- Casts, Endianness and Padding and alignment
- Undefined behavior catalog
- Tutorial: Unions and Padding and alignment