------- Additional Comments From gcc2eran at tromer dot org 2005-07-03 01:19 ------- (In reply to comment #22) > | int foo; > | *(volatile int*) (&foo); > > It was included in my previous message.
Then it's still eluding me, since your foo (the "object"?) was volatile. > | In other words, why should the compiler bother at all with the qualifiers of > | what the pointer "really" points to? > > Because the language definition says that the compiler should preserve > a semantics and the compiler better bothers. Of course, but please forgive my ignorance -- where does does the standard prescribe these semantics? The N869 draft (I don't have the standard; is N869 best fall-back?) says this: "[6.7.3/6] An object that has volatile-qualified type may be modified in ways unknown to the lementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3. [...]" All other references to the semantics volatile likewise talk about objects. So, what is an object? "[3.15/1] object: region of data storage in the execution environment, the contents of which can represent values" "[3.15/2] NOTE When referenced, an object may be interpreted as having a particular type; see 6.3.2.1." What indeed is the type of an object? "[6.3.2.1/1] [...] When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. [...]" And also, later on: "[6.5.3.2/4] The unary * operator denotes indirection. If the operand [...] points to an object, the result is an lvalue designating the object. If the operand has type pointer to type, the result has type type. [...]" And just to be sure about whether "volatile" is part of the type thus specified by the lvalue: "[6.2.5/26] [...] The qualified or unqualified versions of a type are distinct types [...]." So the way I read it, the object designated by *(volatile int*)(anything) has a volatile-qualified type and should thus be evaluated strictly according to the rules of the abstract machine. > but that is a fundamental departure from the language semantics. > Replace "volatile" with "const" -- both are qualifiers -- and observe > the disaster that would ensue. I must be showing my ignorance again, but what disaster would that be? BTW, according to your interpretation, what is the standard-compliant behavior of the following? void quux(int *bar) { *bar = 42; } volatile int foo; quux((int*)&foo); I'm asking because as of 4.0.0, the assignment is optimized away by -O3. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22278