Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Inspired by https://meta.stackoverflow.com/questions/432242, and the relevant main-space questions. I have heard that C and C++ something called the "strict aliasing rule", which means for exa...
#1: Initial revision
How does the strict aliasing rule enable or prevent compiler optimizations?
<section class="notice"> *Inspired by https://meta.stackoverflow.com/questions/432242, and the relevant main-space questions.* </section> I have heard that C and C++ something called the ["strict aliasing rule"](https://stackoverflow.com/questions/98650), which means for example that if we have code like: ```c int aliased(float *f, int *i) { *f = *(reinterpret_cast<float*> i); return *f; } ``` then we have invoked undefined behaviour. I also heard that character types are a loophole. That is, if we type-pun to (`char *`, [`signed char *` or `unsigned char *`](https://stackoverflow.com/questions/40575116)) then we can legally assign bytes from the underlying storage of the `int` to the underlying storage of the `float`. I was told that this has implications for compiler optimizations. [For example](https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1), here the compiler can treat `return *a - t` as if it were just `return 0`: ```c int f (int *a, long *b) { int t = *a; *b = 0; // cannot change *a return *a - t; // can be folded to zero } ``` But [on the other hand](https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2), if we change how `b` is used (braces added for clarity): ``` int f (int *a, long *b) { int t = *a; for (int i = 0; i != sizeof *b; ++i) { ((unsigned char*)b)[i] = 0; } return *a - t; // must not be folded } ``` Now the same optimization is invalid. What is the underlying reasoning here? It seems to me like `a` and `b` are either compatible pointer types or they aren't. Why does code inside the function change the compiler's reasoning? And how does undefined behaviour enable the compiler to make these kinds of optimizations?