Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

60%
+1 −0
Q&A How does the strict aliasing rule enable or prevent compiler optimizations?

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 answer  ·  posted 16h ago by Karl Knechtel‭  ·  last activity 16h ago by Karl Knechtel‭

#1: Initial revision by user avatar Karl Knechtel‭ · 2024-11-16T20:34:15Z (about 16 hours ago)
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?