주요 콘텐츠

AUTOSAR C++14 Rule A3-9-1

R2026b

Fixed width integer types from <cstdint>, indicating the size and signedness, shall be used in place of the basic numerical types

Description

Fixed width integer types from <cstdint>, indicating the size and signedness, shall be used in place of the basic numerical types.

Polyspace Implementation

Only allows use of basic types through direct typedefs.

Rationale

The sizes of the basic numerical types such as char, signed char, and unsigned char are implementation-defined and can vary across platforms and compilers. Code that relies on these types directly may behave differently depending on the target platform, making it non-portable and potentially unsafe.

The fixed-width integer types from <cstdint> such as std::int8_t and std::uint8_t have an explicit, guaranteed size and signedness. Using them in place of the basic types makes the intent of the code clear and ensures consistent behavior across platforms, which is critical for safety-critical embedded and automotive software.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cstdint>

void process(unsigned char data) {    // Noncompliant
}

void store(char buffer[], int size) { // Noncompliant <!--POLYSPACE_MULTIPLE_DEFECTS:2-->
}

void process_fixed(std::uint8_t data) {              // Compliant
}

void store_fixed(std::uint8_t buffer[], std::int32_t size) { // Compliant
}

In this example, the parameters data and buffer in the first two functions use the basic types unsigned char and char. These are noncompliant because their sizes are implementation-defined. The compliant versions use std::uint8_t and std::int32_t from <cstdint>, making size and signedness explicit.

Check Information

Group: Basic Concepts
Category: Required, Automated
PQL Name: std.autosar_cpp14.A3_9_1

Version History

Introduced in R2019a

expand all