在以下几行中,我收到了来自 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 对“零”的解释:
例外
"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."
哪个检查器是正确的?