Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is writing to memory an observable behaviour?

Tags:

c++

c++11

I've looked at the standard but couldn't find any indication that simply writing to memory would be considered observable behaviour. If not, that would mean the compiled code need not actually write to that memory. If a compiler choose to optimize away such access anything involving mapper memory, or shared memory, may not work.

1.9-8 seems to defined a very limited observable behaviour but indicates an implementation may define more. Can one assume than any quality compiler would treat modifying memory as an observable behaviour? That is, it may not guarantee atomicity or ordering, but does guarantee that data will eventually be written.

So, have I overlooked something in the standard, or is the writing to memory merely something the compiler decides to do?

Statements from the current or C++0x standard are good. Please note I'm not talking about accessing memory through a function, I mean direct access, such as writing data to a pointer (perhaps retrieved via mmap or another library function).

like image 412
edA-qa mort-ora-y Avatar asked Jul 31 '11 23:07

edA-qa mort-ora-y


1 Answers

This kind of thing is what volatile exists for. Else, writing to memory and never apparently reading from it is not observable behaviour. However, in the general case, it would be quite impossible for the optimizer to prove that you never read it back except in relatively trivial examples, so it's not usually an issue.

like image 59
Puppy Avatar answered Sep 20 '22 21:09

Puppy