When I today read the C Standard, it says about side effects
Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects
and the C++ Standard says
Accessing an object designated by a volatile glvalue (3.10), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects
Hence because both forbid unsequenced side effects to occur on the same scalar object, C allows the following, but C++ makes it undefined behavior
int a = 0;
volatile int *pa = &a;
int b = *pa + *pa;
Am I reading the specifications correctly? And what is the reason for the discrepancy, if so?
*pa
is an lvalue designating avolatile
qualified object. – Seuratint
. Interestingly if you would have usedvolatile int *pa = calloc(sizeof *pa, 1);
the effective type would volatile. – Seuratint i = 0; int b = i++ + i++; // i = 2 now
, butvolatile int i = 0; int b = i++ + i++; // i = 1 now
. I believe that all operations on avolatile
operand are pooled together across a single assignment. In C++ this is explicitly left undefined. – Autoeroticismside effect
here just means that the program is influenced by something outside of the program flow, not necessarily that it has a side effect. Reading the whole section I agree with the interpretation that this is not allowed in C++ as the program can't "wait" for the completed access from two volatile reads at the same time. (don't know about C). – Lethargy