☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

使用便于移植的类型

6.12.8 ID_unportableType
目录 › next › previous

为了确保数据在各种执行环境中都能被正确处理,避免出现偏差,应使用便于移植的类型,如:

  • 用定宽类型代替 short、int、long、long long 等原始类型
  • 用 char16_t、char32_t 代替 wchar_t
  • 避免使用 long double

示例:

short b;        // Non-compliant
long a;         // Non-compliant
wchar_t c;      // Non-compliant
long double d;  // Non-compliant

例中变量的取值范围在不同的平台上可能会有较大差异,应改为:

int16_t a;   // Compliant
int32_t b;   // Compliant, or int64_t
char16_t c;  // Compliant, or char32_t
double d;    // Compliant

依据

ISO/IEC 9899:2011 6.2.5(5) ISO/IEC 9899:2011 7.20 ISO/IEC 14882:2011 3.9.1(2)

参考

MISRA C 2004 6.3 MISRA C 2012 Dir 4.6 MISRA C++ 2008 3-9-2
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.