c - MISRA 9.2 初始化 float 和无符号数组

标签 c arrays static-initialization misra

在下面几行中,我从 Parasoft C/C++ 测试静态分析工具和 IAR Embedded Workbench MISRA 检查器中收到了不同的问题报告:

[1] static unsigned int array_a[30] = {0U};
[2] static float array_b[20] = {0.0f};

Parasoft 静态分析说:

Not all elements of variable ‘array_a’ are initialized.
Not all elements of variable ‘array_b’ are initialized.

IAR Embedded Workbench 对上述语句没有问题(并且其 MISRA 检查器已打开)。

我可以让变量“array_b”的消息消失:

[3] static float array_b[20] = {0};

但是,同样的技巧对“array_a”不起作用:

[4] static unsigned int array_a[30] = {0};

现在 IAR Embedded Workbench MISRA 检查器报错,因为有符号整数常量 0 被分配给无符号整数:

Error[Pm127]: a 'U' suffix shall be applied to all constants of 'unsigned' type (MISRA C 2004 rule 10.6)

Parasoft 静态分析没有显示上面第 4 行的任何问题。

我相信这归结为对 MISRA 规则 9.2 的“零”的解释:
异常(exception)情况

"All the elements of arrays or structures can be initialized (to zero or NULL)
by giving an explicit initializer for the first element only. If this method
of initialization is chosen then the first element should be initialized
to zero (or NULL), and nested braces need not be used."

哪个检查器是正确的?

最佳答案

Parasoft Static Analysis says:

Not all elements of variable ‘array_a’ are initialized.

这是不正确的。所有元素都已初始化,代码不违反 MISRA-C 9.2,它明确允许零初始化,只需将数组的一项设置为零。

static float array_b[20] = {0};

严格来说,这不符合 MISRA 标准,因为所有整数文字都必须像您的第一个代码中那样是无符号的。更改为 0u0U 或者 0.0f(后者最有意义)。

static unsigned int array_a[30] = {0};

此处相同,不符合 MISRA。

The Parasoft Static Analysis is not showing any issues for line 4 above.

在所有提到的情况下,Parasoft 似乎都是不正确的。您应该在他们的 MISRA 检查器中将此报告为错误。

在所有提到的情况下,IAR 似乎都是正确的。

关于c - MISRA 9.2 初始化 float 和无符号数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21442539/

相关文章:

c - 如何理解d_hash()的函数?

c - 返回指向字符串的指针数组

c - Rand() 不在 C 中生成随机变量

javascript - 检查至少两个数组值是否相等

c++ - 静态初始化数组 union 结构

c - 死锁 - 转移程序

c - 确定 C 的打印

c++ - 无法在 Intel Xeon Phi 上执行二进制错误

c++ - Static Initialization Order Fiasco 为什么这样称呼?

java - 静态初始值设定项不能在定义之前引用字段