Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
13.5 clarification on loop index iusage
#2
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.
<t></t>


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)