简体   繁体   English

关于语句操作的 Misra 违反 10.1 (MISRA C 2012)

[英]Misra Violation 10.1 regarding statement operations (MISRA C 2012)

i need some help with a piece of code.我需要一段代码的帮助。 I have an array, lets call it array[4].我有一个数组,我们称它为数组 [4]。 Now I want to check that at least 3 elements of this array are taller than a threshold.现在我想检查这个数组的至少 3 个元素是否高于阈值。 (if statement) (如果声明)

Eg例如

if(2 > ((array[0] > threshold) + (array[1] > threshold) + (array[2] > threshold) + (array[3] > threshold) ))

Here Misra is complaining.米斯拉在这里抱怨。 (Rule 10.1 Unpermitted operand operator "+") Is there another way to code this if statement without checking every possible permutation? (规则 10.1 未经许可的操作数运算符“+”)是否有另一种方法来编写这个 if 语句而不检查所有可能的排列?

Cheers干杯

How about unpacking the one-liner, possibly to a loop?拆开单衬里的包装怎么样,可能是一个循环? It could be more readable too:它也可能更具可读性:

int check = 0;
for (int i = 0; i<4; i++) {
  if (array[i] > threshold) {check++;}
}
if (check >= 3) ...

Your if statement actually appears to be testing something else "at least 3 are taller" vs if (2 >...) (at most one?).您的 if 语句实际上似乎是在测试“至少 3 个更高”与if (2 >...) (最多一个?)。

At the heart of this question is a misunderstanding... that a boolean true is of the value 1 and false is 0 - and that you can add three booleans together.这个问题的核心是一个误解...... boolean true的值为1false0 - 您可以将三个布尔值相加。

A boolean is either true or it is false . A boolean 要么是true要么是false

Mathmatically, the + operator makes no sense with a boolean: and that is the rationale for this particular MISRA Rule (see also the Appendix explaining Essential Types )... it doesn't help that the C implementation of boolean is so broken.从数学上讲, +运算符对 boolean 没有意义:这就是这个特定 MISRA 规则的基本原理(另请参见解释基本类型的附录)...... Z84E2C64F38F78BA7ZA 的 C 实现如此损坏并没有帮助。905AB5F78BA7ZA5

The other answers offer alternatives.其他答案提供了替代方案。 But one plea, spare us the yoda conditionals, especially as you appear to have it the wrong way around for your explanation... if ( 2 >... ) is not even your false condition, it needs to be if ( 2 <... )但是有一个请求,请不要使用 yoda 条件句,尤其是当您的解释似乎有错误的方式时…… if ( 2 >... )甚至不是您的错误条件,它必须是if ( 2 <... )

The MISRA warning is because you attempt to do arithmetic on types that are "essentially boolean" which is nonsensical. MISRA 警告是因为您尝试对“本质上是布尔值”的类型进行算术运算,这是无意义的。

But that's the least of your problems, this line of code is plain awfully written, to the point where one will suspect deliberate obfuscation.但这是你的问题中最小的一个,这行代码写得非常糟糕,以至于人们会怀疑故意混淆。 It does not "check that at least 3 elements of this array are taller than a threshold".它不会“检查该数组的至少 3 个元素是否高于阈值”。 It can't be salvaged.它无法挽救。

One way to re-write this code is (MISRA-C:2012 compliant):重写此代码的一种方法是(符合 MISRA-C:2012):

uint32_t above_count=0;
for(size_t i=0; i<N; i++) // where N is some named variable for the amount of items / array size etc
{
  if(array[i] > threshold)
  {
    above_count++;
  }
}

if(above_count >= 3) // "check that at least 3 elements of this array are taller than a threshold"
{
  ...
}

How about explicitly converting those pesky non-addable booleans to integer 1/0?:如何将那些讨厌的不可添加布尔值显式转换为 integer 1/0?:

if(2 > ((array[0] > threshold?1:0) + (array[1] > threshold?1:0) + (array[2] > threshold?1:0) + (array[3] > threshold?1:0) ))

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

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