| ??? 01/23/03 07:38 Read: times |
#36978 - RE: volatile keyword |
So if I have
void foo(void) { volatile unsigned char i; i = 3; } Then it's 100% guaranteed that the statement i=3 will be compiled into code by the compiler 100% of the time regardless of what optimization is in use? Andy Michael Karas wrote: ------------------------------- The volatile key word is applied to any variable that when its value is written or referenced in C code, that the actual value in that variable may change for some reason before the next time the variable is referenced by the C code. For example..if you had... int x; int y; void func(void) { x=13; y=45; y=y*2; y=y+some_subroutine(y); y=y+x; } ....if there was a chance that the X value could change by the time you got the last statement then you would want to declare x with the volatile attribute. The precise reason this is important is because with code like above the compiler may have decided to keep the 13 value of x in a register down through the code sequence and then used that registered value instead of going back to the x memory location for the value. A compiler with no optimization would keep the x & y memory locations up to date statement by statement. However the modern compilers today, in attempting to make code smaller and smaller will try to keep intermediate values in local storage (ie registers) across several statements in order to make the code more efficient and smaller. When you apply the volatile keyword to a variable you are telling the compiler to go back to reference the actual storage for the variable on a statement by statement basis. Michael Karas |
| Topic | Author | Date |
| volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
| RE: volatile keyword | 01/01/70 00:00 | |
volatile keyword and not-so-optimizer | 01/01/70 00:00 |



