Hi there All, I have a problem which seemed to be defying explanation, but I have come up with a theory. Could I possibly have some feedback on whether the following is likely, plausible, possible, untrue or downright rubbish? If one reads the contents of a CAN or ADC chip register at a particular address, then the label volatile is placed upon that address to prevent the compiler optimising out repeat readings of the address. If one reads the contents of the address into a variable, then the compiler would automatically treat the contents of this variable with similar care. Is it possible that there has been an oversight with statements where the contents of a variable depend on the contents of a volatile by way of an if statement, ie...
normal_var=volatile_var;
normal_var=voltile_var; if (normal_var=0x00) { another_normal_var+=1; }
some snippets of this code... Here one can see - nothing has been put in the register Here. In this particular case. So yes, this is an example that omitting volatile where you should have used it doesn't automatically guarantee your code to fail. So what else is new? Examples don't prove anything, period. Let me repeat that: PROOF BY EXAMPLE DOESN'T WORK. [Sorry for the shouting, everybody, but it seems there's no other way we'll get the message into this person's mind.]
Examples don't prove anything, period. Let me repeat that: PROOF BY EXAMPLE DOESN'T WORK. [Sorry for the shouting, everybody, but it seems there's no other way we'll get the message into this person's mind.] Hans-Bernhard, I do not know how many posts we have seen stating "it tested" and I am frustrated, just like you, that so many believe in "testing". Succesful testing does not prove the abscence of bugs, it only proves the abscence of known bugs. The analogy would be that because when you did not stop at a stop sign you did not cause an accident it can be concluded that there is no risk ignoring that stop sign. I wonder how this point can get across and really do appreciate to see you phrasing it in a different form, so maybe one of the many can understand that form. Erik
I'm a little confused on how using a volatile type qualifier protects you Volatile does not provide mutual exclusion. It does not "protect" you in that sense. Volatile informs the code generator that this value may change for reasons unknown to the code generator, and thus many common optimizations are unsafe. The variable address should always be read or written. Masking interrupts to provide mutual exclusion can help avoid some problems by making sure the flawed code never executes in a manner that will expose the flaw. In other cases, though, it won't help. Consider the hoisting of global_refresh example I posted earlier in the thread. Even if you make some mutex function calls inside the loop, there's still no reason for the compiler not to hoist the read completely outside the loop (and the mutex), and you can get the wrong results despite the "protection" from the critical section. The only way the compiler can understand the usage of the variable is if you declare it volatile In other cases there's no substitute. The classic example would be something like a programmed I/O register. You copy a frame out of the single PIO address by reading it multiple times in a loop. For an ordinary variable at an ordinary address, most code generators will generate only one actual read, and then reuse the value kept in a register. But to make the hardware work correctly, you need actual physical transitions on the bus. Each read has to actually hit that memory address, and each value read from the "same" PIO port will be different. The compiler can't just use the old value, because that's a different byte in the frame. Volatile is not an optimization hint that can be ignored, like register. It is necessary to specify a constraint on the code generator to get correct code.
hello, the question was not to proof by example that's work! It's clear that one should use Volatile declaration. About "when" and "how" one can read in many helps. The question was how it worked in this concrete case without this declaration and without "may be" or it was a miracle or luck and so on. I would hear it like , it works so because... or it has been read from the same memory location each time, because... I would't need common explanation which are clear for everybody, I would need it a little more deeper in the problem. Sorry, if somehow straight. With best wishes to all and thank you very much. S.