AutosarC++18_10-A18.1.1

C-style arrays should not be used

Required inputs: IR

Rationale

A C-style array is implicitly convertible to a raw pointer and easily loses information about its size. This construct is unsafe, unmaintainable, and a source of potential errors.

For fixed-size, stack-allocated arrays, std::array is supposed to be used instead.

This type is designed to work with standard library algorithms.

Exceptions

It is allowed to declare a static constexpr data member of a C-style array type.

Example
// $Id: A18-1-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
    #include <algorithm>
    #include <array>
    #include <cstdint>
    void Fn() noexcept
    {
        const std::uint8_t size = 10;
        std::int32_t a1[size];              // Non-compliant
        std::array<std::int32_t, size> a2; // Compliant
        // ...
        std::sort(a1, a1 + size);
        std::sort(a2.begin(), a2.end()); // More readable and maintainable way of
                                          // working with STL algorithms
    }
    class A
    {
      public:
         static constexpr std::uint8_t array[]{0, 1, 2};     // Compliant by exception
    };
See Also
  • C++ Core Guidelines [11]: ES.27: Use std::array or stack_array for arrays on the stack.
  • C++ Core Guidelines [11]: SL.con.1: Prefer using STL array or vector instead of a C array.

Possible Messages

Key

Text

Severity

Disabled

c_array_used

C-style arrays should not be used.

None

False

Options

allowed_in_variables_of_type

allowed_in_variables_of_type : set[bauhaus.ir.PIR_Class_Name] = {'Constexpr_Static_Field_Declaration', 'Constexpr_Static_Field_Definition'}

PIR class names of declarations and definitions in which C-style arrays are allowed.