Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is accessing volatile local variables not accessed from outside the function observable behavior in C++?

In C++03 Standard observable behavior (1.9/6) includes reading and writing volatile data. Now I have this code:

int main()
{
    const volatile int value = 0;
    if( value ) {
    }
    return 0;
}

which formally initializes a volatile variable and then reads it. Visual C++ 10 emits machine code that makes room on the stack by pushing a dword there, then writes zero into that stack location, then reads that location.

To me it makes no sense - no other code or hardware could possibly know where the local variable is located (since it's in automatic storage) and so it's unreasonable to expect that the variable could have been read/written by any other party and so it can be eliminated in this case.

Is eliminating this variable access allowed? Is accessing a volatile local which address is not known to any other party observable behavior?

like image 553
sharptooth Avatar asked Sep 26 '11 09:09

sharptooth


People also ask

What is volatile variable in C where it is used?

A volatile keyword in C is nothing but a qualifier that is used by the programmer when they declare a variable in source code. It is used to inform the compiler that the variable value can be changed any time without any task given by the source code. Volatile is usually applied to a variable when we are declaring it.

How does volatile keyword work in C?

The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time.

Where is volatile keyword stored?

There's no reason for a volatile variable to be stored in any "special" section of memory. It is normally stored together with any other variables, including non-volatile ones. If some compiler decides to store volatile variables in some special section of memory - there's nothing to prevent it from doing so.

Does local variable get destroyed?

Local Variable: These variables are declared within a method but do not get any default value. They are usually created when we enter a method or constructor and are destroyed after exiting the block or when the call returns from the method.


1 Answers

The thread's entire stack might be located on a protected memory page, with a handler that logs all reads and writes (and allows them to complete, of course).

However, I don't think MSVC really cares whether or how the memory access might be detected. It understands volatile to mean, among other things, "do not bother applying optimizations to this object". So it doesn't. It doesn't have to make sense, because MSVC is not interested in speeding up this kind of use of volatile.

Since it's implementation-dependent whether and how observable behavior can actually be observed, I think you're right that an implementation can "cheat" if it knows, because of details of the hardware, that the access cannot possibly be detected. Observable behavior that has no physically-detectable effect can be skipped: no matter what the standard says, the means to detect non-conforming behavior are limited to what's physically possible.

If an implementation fails to conform to the standard in a forest, and nobody notices, does it make a sound? Kind of thing.

like image 103
Steve Jessop Avatar answered Nov 14 '22 21:11

Steve Jessop