I believe volatile force the compiler to issue a real write to memory and can't be optimized away.
Re:Being Sneaky in C
by TheLoneWolfling on Friday June 12, 2015 @07:58PM
(#9709280)
Except, of course, that the compiler can perfectly legally copy the variable behind the scenes and not overwrite the copy.
I mean it when I say you cannot.
I'm not sure what you mean here, if you have a volatile pointer that points to a memory buffer returned by malloc, how can the compiler prevents a write through the pointer from happening?
Edit: unless your point is a temporary copy can be spilled in memory and this copy will stay in memory and won't be overwritten?
Re:Being Sneaky in C
by TheLoneWolfling on Friday June 12, 2015 @10:00PM
(#9709576)
Yes, your edit is correct.
Why not generate random data then read it? Or even a constant, then read it?
Re:Being Sneaky in C
by TheLoneWolfling on Friday June 12, 2015 @04:55PM
(#9708478)
Because the compiler will optimize it out. Even if you return the random data / constant the compiler will optimize out the store to the variable and just pass it through directly.
There is no way to ensure that something is actually overwritten, because under the memory models of C and C++ you cannot ever read that memory again, even though in actuality you can.