volatile semantics

Geoff Keating geoffk@geoffk.org
Sat Jul 23 09:50:00 GMT 2005


On 22/07/2005, at 7:15 PM, Paul Schlie wrote:

>> Geoffrey Keating writes:

>> without 'volatile', then this object cannot be modified unknown to  
>> the
>> implementation, even if someone also writes '(*(volatile int *)&i)  
>> = 1'.
>>
>
> - merely means: treat the object being referenced as volatile  
> qualified int
>   object (as the standard specifies, although it may result in an  
> undefined
>   behavior, nothing more or less; as although the object may have  
> not been
>   initially declared as being volatile, the program within the  
> context of
>   this particular references has asserted that it must be treated  
> as such,
>   thereby implying it's value must be assigned, and/or presumed to  
> have been
>   possibly modified beyond the logical view of the program).

It doesn't imply that.  All it implies is that *from this access* the  
compiler cannot assume that the object is not "modified in ways  
unknown to the implementation".  From other accesses, including from  
the original declaration (and its initializer if any), the  
implementation may be able to make that deduction.  If, and only if,  
the implementation can make that deduction, then it can perform  
optimizations.  In this example:

int i = 0;
while (*(volatile int *)&i == 0) ;

then the implementation can make that assumption, and optimise the  
loop into an infinite loop that does not test 'i', because the '= 0;'  
performs a store to a non-volatile object 'i' which therefore cannot  
be modified in ways unknown to the implementation and therefore will  
always be zero.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2410 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20050723/db3062c7/attachment.p7s>


More information about the Gcc mailing list