What is Strict Aliasing and Why do we Care? The article explains that strict aliasing is a rule in C and C++ that specifies which types of pointers can legally access the same memory location, allowing compilers to optimize code under the assumption that these rules are followed. Violating strict aliasing results in undefined behavior, which can cause programs to produce unexpected results, especially when compiler optimizations are enabled. The article provides examples, such as accessing an `int` object through a `float*`, to demonstrate how such violations can lead to incorrect optimizations and unreliable program behavior. What is the Strict Aliasing Rule and Why do we care? OR Type Punning, Undefined Behavior and Alignment, Oh My What is strict aliasing? First we will describe what is aliasing and then we can learn what being strict about it means. In C and C++ aliasing has to do with what expression types we are allowed to access stored values through. In both C and C++ the standard specifies which expression types are allowed to alias which types. The compiler and optimizer are allowed to assume we follow the aliasing rules strictly, hence the term strict aliasing rule . If we attempt to access a value using a type not allowed it is classified as undefined behavior http://en.cppreference.com/w/cpp/language/ub UB . Once we have undefined behavior all bets are off, the results of our program are no longer reliable. Unfortunately with strict aliasing violations, we will often obtain the results we expect, leaving the possibility the a future version of a compiler with a new optimization will break code we thought was valid. This is undesirable and it is a worthwhile goal to understand the strict aliasing rules and how to avoid violating them. To understand more about why we care, we will discuss issues that come up when violating strict aliasing rules, type punning since common techniques used in type punning often violate strict aliasing rules and how to type pun correctly, along with some possible help from C++20 to make type punning simpler and less error prone. We will wrap up the discussion by going over some methods for catching strict aliasing violations. Preliminary examples Let's look at some examples, then we can talk about exactly what the standard s say, examine some further examples and then see how to avoid strict aliasing and catch violations we missed. Here is an example that should not be surprising live example https://wandbox.org/permlink/7sCJTAyrifZ0zfFA : cpp int x = 10; int ip = &x; std::cout << ip << "\n"; ip = 12; std::cout << x << "\n"; We have a int\ pointing to memory occupied by an int and this is a valid aliasing. The optimizer must assume that assignments through ip could update the value occupied by x . The next example shows aliasing that leads to undefined behavior live example https://wandbox.org/permlink/8qA8JyJRVHtS9LPf : cpp int foo float f, int i { i = 1; f = 0.f; return i; } int main { int x = 0; std::cout << x << "\n"; // Expect 0 x = foo reinterpret cast