I was perusing Danny's article (http://www.informit.com/guides/conte...lus&seqNum=345) and was reminded of a question.

I've always used volatile in the context of a pointer: what the pointer points to changes due to other threads. We use something like:
volatile int* ptr=...;

In this case, the value in "ptr" is *not* volatile, rather, the value pointed to by the value in ptr.

One might have a need for:
volatile int my_int;

In which case the value in my_int needs to be volatile.

My confusion is this: how does the compiler know to not optimize *ptr in one case an my_int in another?

Question number two. Would this work:
volatile int& alias_ptr(*ptr);
And the reference be correctly handled?