当前位置:网站首页>1216_ MISRA_ C standard learning notes_ Rule requirements for control flow

1216_ MISRA_ C standard learning notes_ Rule requirements for control flow

2022-04-23 08:07:00 grey_ csdn

All learning summary : https://github.com/GreyZhang/misra_c_hacking

There can be no code that cannot be executed , Except for defensive codes . About what defensive code is , In the following rules, you can continue to understand . For the various projects I'm dealing with now , In fact, the biggest conflict in this part may be that the calibration selection enables the branch to take effect . This part can indeed be detected at the compilation stage , However, in practical application, it should be retained .

All non empty expressions can either produce some effective effect , Either it can affect the selection or change of program flow . Simple invalid handling is not allowed .

An empty statement must be followed by at least one space , Then you can add notes . in addition , You cannot use empty statements after comments .

These requirements are not explained directly , Because it's direct enough . First , Can't use goto; secondly , Can't use continue; also , Each cycle can only have at most one break For the end of the loop . So far , The projects I came into contact with may not completely deal with such requirements . Some protocol stacks in legacy code seem to have goto, And I used it myself when designing software continue. in addition , I seem to have seen many break The situation of . Actually , If you comply with these requirements, there is also a way to solve , Adding some state variables and conditional branches can easily solve .

A function can only have an exit point at the end of the function . Actually , I've seen open source before 1939 The agreement seems to have done a good job in this regard .

Various cycles and conditional structures , It is basically right to use curly braces as much as possible .

The best time to meet this rule should be combined with what is written above , Try to handle conditional branches with curly braces . in addition , every last if Should be one else end . And this else It's the defensive programming mentioned earlier , If it really can't be implemented , Then write a note here to explain the reason . This one was mentioned by others in my work many years ago, and it was printed in my own memory , I didn't write much code at that time , But today, these requirements have solidified into my own coding habits .

版权声明
本文为[grey_ csdn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230645316190.html