On Tue, Jun 17, 2008 at 08:51:24PM -0700, Karen Shaeffer wrote:
> I see your point. My sticking point is that the process is actually
> running on a physical machine.

And that's the problem.  You, like many C programmers, have in your head a
physical machine model where pointer variables are physical machine
addresses and other variables are physical memory locations.  But C
doesn't necessarily work that way (except at -O0); if it did, programs
would run substantially slower.  The compiler can make optimizations (like
reusing values in registers) in certain circumstances.  Anywhere in the
standard where you see that a behavior is undefined, read that as meaning,
the compiler is allowed to produce an arbitrary code sequence, so it will
produce the fastest, or smallest, or simplest-to-implement sequence.

> And the addresses, although virtual,
> do translate to a unique physical memory location. And, the value
> stored in that location cannot be 0 and 5 at the same time.

Right, but since it's const, the compiler is allowed to assume that it
is, well, const!  That means that if it has already read the value into
a register, it can assume that the register is still valid for the
remainder of the program execution, even if you decide to violate the
rules of C and change the "constant".  That's why it appears to be
0 and 5 at the same time.

To get rid of that behavior, you'd have to force the compiler to read
every variable from memory every time, and not use registers at all.

Reply via email to