13.5 clarification on loop index iusage - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.13 Control Statement Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=40) +---- Thread: 13.5 clarification on loop index iusage (/showthread.php?tid=577) |
13.5 clarification on loop index iusage - Alexandre Langenieux - 04-09-2008 Dear Misra Committee, Rule 13.5 seems not clearly explicit about the content of the for expressions.
Re: 13.5 clarification on loop index iusage - Lundin - 04-09-2008 My opinion is that MISRA 13.5 could be formulated clearer. It says that the first expression of the loop should only initialize the loop counter. But if we are speaking of strict programming terms, initialization refers only to: int i = 0; and not to int i; i=0; The first example is initialization, the second one is assignment. The difference is very important if the code for example is ported to C++, where there is a big difference between initialization and assignment. C++ programmers would definitely only consider the first example as initialization, and it is safe to assume that plenty of C++ programmers end up writing C programs. C++ also allows initialization within the for-loop, as it allows variable declarations there. C does not. Perhaps the MISRA rule should be formulated as this: "Fist expression Assigning a value to the loop counter by using the assignment operator (=)." --- Regarding the second expression in the original post: calling a function from the test part of the loop is usually not smart performance-wise. You will get the function calling overhead each time you run the loop. Though as long as the counter test code is clear, I don't see how it would make the code less safe, which should be MISRA's only concern. Re: 13.5 clarification on loop index iusage - misra-c - 24-09-2008 These forms of loop are far from ideal, but do comply with the requirements of Rule 13.5 as written. Use of for loops in this way will be reviewed and may be deprecated in subsequent versions of MISRA C. [ID:0000011] |