MISRA Discussion Forums
6-5-4: on variable or expression? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.6 Statements (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=135)
+---- Thread: 6-5-4: on variable or expression? (/showthread.php?tid=1561)



6-5-4: on variable or expression? - Nadege - 29-01-2021

Hello,

6-5-4 says "The loop-counter shall be modified by one of: --, ++, - = n, or + = n; where n remains constant for the duration of the loop"

Should this code below raise 6-5-4?

int main() {
int k = 8;

for (int j = 0; j < 10; j += k + 3) {
}
}

If 'n' concerns variable only, I would say 'Yes'.
If 'n' concerns expressions, I would say 'No'.

What do you think?
Thanks in advance for your help.

Nadège


Re: 6-5-4: on variable or expression? - misra cpp - 05-02-2021

We'd expect the example to be compliant.

In the examples for 6-5-4, the 4th shows 'n' as a non-const variable, and its labelled as 'Compliant if 'n' is not modified within the body of the loop'. So there is an expectation that an analyzer will accept a non-const variable as 'n', and will perform analysis to show that it is not modified. How far that analysis tracks say pointers to n or references to n passed to functions in the loop body is a matter of quality of implementation.