Main Content

MISRA C:2012 Dir 4.6

typedefs that indicate size and signedness should be used in place of the basic numerical types

Description

Directive Definition

typedefs that indicate size and signedness should be used in place of the basic numerical types1 .

Rationale

When the amount of memory being allocated is important, using a type that indicates its lengths makes it clear how much storage is being reserved for each object.

Polyspace Implementation

Polyspace® considers integer, float, and complex types as basic numeric types. A violation is reported if the data type for basic numeric types do not include a valid size in bits or contain commonly used size-related words. Valid sizes in bits are multiples of eight. Supported size-related words are bit, byte, word, and other common keywords.

Polyspace checks data types in function prototypes, type declarations, variable declarations, cast expressions, and the sizeof operator.

Polyspace does not report a violation on these uses:

  • Use of complex and imaginary types in cast conversions.

  • Use of the types size_t and ptrdiff_t.

  • Use of plain char.

  • Use of int as the return type of the main() function.

  • Use of int as the type for a parameter of the main() function.

  • Use of known boolean types such as bool or _Bool.

The header stdint.h defines some fixed width types that represent the smallest or the fastest possible size of a type. Examples include int_least8_t, unit_fast_8_t, and similar types. See Fixed with integer types. Polyspace reports a violation if you use these types. For example, this code declares the 24bit variable var_globe24 as an integer that has at least 8 bits.

int_least8_t var_glob24; 
Because the type does not represent the size of the variable correctly, Polyspace reports a violation.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

This example shows compliant and noncompliant typedefs of basic numeric types. Polyspace reports a violation if the typedef of the basic numeric type does not specify a valid size in bits. Specifying the size by using keywords such as word and byte is compliant with this rule.

typedef unsigned int uint32_t;     /* Compliant */
typedef unsigned short WORD;       /* Compliant */
typedef unsigned int DWORD;        /* Compliant */
typedef unsigned long qword;       /* Compliant */
typedef unsigned int u3_t;         /* Noncompliant */

int x = 0;                         /* Noncompliant */
uint32_t y = 0;

Check Information

Group: Code design
Category: Advisory
AGC Category: Advisory

Version History

Introduced in R2014b

expand all


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.