Re: [GD-General] The joy of type aliasing and C
Brought to you by:
vexxed72
From: Tom H. <to...@ve...> - 2003-12-26 22:10:50
|
At 01:01 PM 12/26/2003, you wrote: >Turns out it is. An assignment expression or function call is treated >as a "void expression" which is evaluated specifically for its side >effects and thus should count as a full expression and, by extension, >as a sequence point. > >So if this chokes an optimizer: > >x = * ( int * ) &f; > >Due to an assignment that occurs on 'f' earlier not being done until >after the assignment to 'x', then it is the optimizer's fault, and not >the programmers. Maybe someone else on the list can confirm what we've been talking about off-list. This problem only exists if the "Assume no Aliasing" compiler optimization is turned on. My understanding is that this optimization tells the compiler that it is ok to break the rules. If the programmer then uses aliasing after telling the compiler to assume he isn't, then I would call this a programmer error ;) The sticky part is, I believe this optimization is defaulted to on in most release build configurations, so it's one of those things that most programmers probably aren't aware of. In one of the many threads on this subject, it has been said that the C++ spec has something in it that allows for this optimization (specifically, the C++ spec says that the compiler can assume no aliasing). The end result would be that writing the above code in C++ is illegal with or without the optimization turned on. Anyone have a copy of the C++ spec that they can search and post relevant portions? >Not to say that a programmer shouldn't be aware of this stuff and deal >with it properly, of course =) Agreed. Tom |