简体   繁体   English

MISRA 2012规则14.2

[英]MISRA 2012 Rule 14.2

I have a question related to MISRA 2012 Rule 14.2 "A for loop shall be well-formed" 我有一个与MISRA 2012规则14.2有关的问题“for for循环应该是格式良好的”

Conside below sample code : 考虑以下示例代码:

int foo (int *ptr)
{
    (*ptr)--;
     return *ptr;
}

void main()
{
    int a =20;
    int i;
    for (i=0; i< foo(&a) ; i++)
    {
         /*
         <loop body>
         */       
    }
}

Here for line for (i=0; i< foo(&a) ; i++) I am getting a MISRA violation, 14.2. 这里换行for (i=0; i< foo(&a) ; i++)我得到MISRA违规,14.2。 The question is when we modify the variable (a) present in the loop condition (i< foo(&a)), in a function like shown. 问题是当我们在如图所示的函数中修改循环条件(i <foo(&a))中存在的变量(a)时。 is it valid violation ? 是有效违规吗?

Its just a sample case, for 14.2, Please do not focus on the loop being infinite in the above sample code. 它只是一个示例案例,对于14.2,请不要关注上面示例代码中的无限循环。


14.2 Rule : Second clause which 14.2规则: 第二条款
- Shall be an expression that has no persistent side effects, and - 应该是一个没有持久性副作用的表达,并且
- Shall use the loop counter and optionally loop control flags, and - 应使用循环计数器和可选的循环控制标志,和
- Shall not use any other object that is modified in the for loop body. - 不要使用for循环体中修改的任何其他对象。

Example :- 示例: -

 bool_t flag = false;
    for ( int16_t i = 0; ( i < 5 ) && !flag; i++ )
    {
    if ( C )
    {
    flag = true; /* Compliant - allows early termination
    * of loop */
    }
    i = i + 3; /* Non-compliant - altering the loop
    * counter */
    }

Your example code violates the quoted rule (first bullet) because 您的示例代码违反了引用的规则(第一个项目符号),因为
it does have side effects (or the compiler cannot really tell, because of calling a function with a prototype which would allow such side effects - and happens to have at least one). 它确实有副作用(或者编译器无法真正告诉,因为调用具有允许这种副作用的原型的函数 - 并且碰巧至少有一个)。

Your example might violate the quoted rule (third bullet) if the (side-) effects of the loop continuation condition ( i< foo(&a) ) are counted (by your specific MISRA analyser) as part of "the loop body". 如果循环连续条件( i< foo(&a) )的(侧面)效应(由您的特定MISRA分析器计算)作为“循环体”的一部分,则您的示例可能违反引用的规则(第三个项目符号)。 (I would not, but your tool might.) (我不会,但你的工具可能。)

So your shown code violates the rule between one and two times. 因此,您显示的代码违反了规则一到两次。

The rationale for Rule 14.2 shows that this Rule is intended to restrict for loops, stopping "clever" uses, and thus make code easier to review and analyse... 对于第14.2条的基本原理表明,该规则旨在限制for循环,停止“聪明”的使用,从而使代码更易于查看和分析...

I have a simple maxim: 我有一个简单的格言:

  • If you have a pre-determinable number of iterations, use a for loop 如果您有预先确定的迭代次数,请使用for循环
  • If you don't have a pre-determinable number of iterations, use a while ... do loop 如果没有预先确定的迭代次数,请使用while ... do循环

Assuming foo(&a) is does not return a constant, you would be better off using a while ... do loop: 假设foo(&a)不返回常量,那么最好使用while ... do循环:

int a = 20;
int i = 0;

while ( i < foo(&a) )
{
  // Loop body
  ...
  ++i;
}

Note: See profile for disclaimer. 注意:请参阅免责声明的个人资料。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM