The Perils of Strict Aliasing
summary
Strict aliasing makes type-punning through differently typed pointers undefined behavior so swapping a uint32 by accessing it as uint16 can silently break; using a union only works as a GCC union copy extension in the copy-in/copy-out pattern, and placement new yields indeterminate values per the standards committee. Workarounds: use memcpy through char pointers (guaranteed), use -fno-strict-aliasing (portable but measurable performance loss observed in market‑data/netcode and the Linux kernel), or use GCC’s may_alias attribute (nonportable).
AI-written summary · about
chapters (8)
- 0:00Introduction
- 1:09C Rules
- 1:39Does this work
- 1:58Not guaranteed to work
- 2:13Unions
- 2:59C99
- 4:25MemCopy
- 5:00Conclusion
related talks
58mArgument Passing, Core Guidelines and Aliasing
07mType punning done right
18mAliasing Limits on Translating C to Safe Rust
01h 00mDealing with aliasing using contracts
06mMy least favorite C++ feature
58mLeveraging C++20/23 Features for Low Level Interactions
42mWhat If We Pretended Unsafe Code Was Nice, And Then It Was?
06mLightning Talk: Un-Undefining Undefined Behavior in C++
