Skip to content
Tutorials Center
My learning

Topics

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.

C
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 (int and unsigned int);
  • a struct or union that 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 typedef for 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

C Safe ways to reinterpret bytes

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.

C Why the compiler may assume no aliasing

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

  • memcpy is the portable fix. Modern compilers turn a fixed-size memcpy into a single register move, so there is no performance cost. Use it for parsing network data and file formats, and to read a float'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 memcpy for code that must compile as both.
  • Alignment is a separate rule. Casting char * data to int * can produce a misaligned pointer, which is undefined even if you never break aliasing. memcpy avoids both problems.
  • Signed and unsigned versions may alias, so *(unsigned *)&signed_int is allowed; the value is just reinterpreted.
  • -fno-strict-aliasing disables 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 -Wall at -O2) catches only the obvious cases.
  • restrict is 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.