strict-aliasing

What is the strict aliasing rule?

不打扰是莪最后的温柔 提交于 2019-11-26 01:17:32
问题 When asking about common undefined behavior in C, people sometimes refer to the strict aliasing rule. What are they talking about? 回答1: A typical situation you encounter strict aliasing problems is when overlaying a struct (like a device/network msg) onto a buffer of the word size of your system (like a pointer to uint32_t s or uint16_t s). When you overlay a struct onto such a buffer, or a buffer onto such a struct through pointer casting you can easily violate strict aliasing rules. So in

Why does optimisation kill this function?

纵然是瞬间 提交于 2019-11-26 01:15:48
问题 We recently had a lecture in university about programming specials in several languages. The lecturer wrote down the following function: inline u64 Swap_64(u64 x) { u64 tmp; (*(u32*)&tmp) = Swap_32(*(((u32*)&x)+1)); (*(((u32*)&tmp)+1)) = Swap_32(*(u32*) &x); return tmp; } While I totally understand that this is also really bad style in terms of readability, his main point was that this part of code worked fine in production code until they enabled a high optimization level. Then, the code