Look at this simple code:
struct Point {
int x;
int y;
};
void something(int *);
int main() {
Point p{1, 2};
something(&p.x);
return p.y;
}
I expect, that main
's return value can be optimized to return 2;
, as something
doesn't have access to p.y
, it only gets a pointer to p.x
.
But, none of the major compilers optimize the return value of main
to 2
. Godbolt.
Is there something in the standard, which allows something
to modify p.y
, if we only give access to p.x
? If yes, does this depend on whether Point
has standard layout?
What if I use something(&p.y);
, and return p.x;
instead?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…