现象:<br />HT PICC18编写的程序,在运行中,出现错误。使用ICD2仿真后,发现进入错误的断点,而断点处的bank选择是错误的。<br />现在所有变量加上volatile关键字后,错误消失,运行正确。源程序没有做修改。<br /><br />问题:<br />volatile关键字到底作用是什么?英文翻译的原意是:挥发性的,可变的。。。但是PICC USER MANUAL里面的说明是:<br />The volatile type qualifier is used to tell the compiler that an object cannot be guaranteed to retain its value between successive accesses. This prevents the optimizer from eliminating apparently<br />redundant references to objects declared volatile because it may alter the behaviour of the program to do so.<br />大概意思应该是防止编译器误优化掉,具体应该怎么理解,请指教。<br /><br />同样,STATIC关键字,怎么理解。<br /><br />最后,是不是所有的程序,变量定义都要使用volatile或者static?怎么做比较好?好像在MISRA C规范里面,所有的变量定义需要static关键字的。 |
|