メインコンテンツ

MISRA C:2023 Rule 9.5

Where designated initializers are used to initialize an array object the size of the array shall be specified explicitly

R2024a 以降

説明

ルール定義

Where designated initializers are used to initialize an array object the size of the array shall be specified explicitly 1 .

根拠

配列のサイズが明示的に指定されていない場合、サイズは初期化された要素の最大のインデックスにより決定されます。指示付きの長い初期化子を使用すると、どの要素が最大のインデックスをもつかすぐにはわからない場合があります。

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

すべて展開する

int a[5] = {[0]= 1, [2] = 1, [4]= 1, [1] = 1};        /* Compliant */
int b[] = {[0]= 1, [2] = 1, [4]= 1, [1] = 1};         /* Non-compliant */
int c[] = {[0]= 1, [1] = 1, [2]= 1, [3]=0, [4] = 1};  /* Non-compliant */

void display(int);

void main() {
    func(a,5);
    func(b,5);
    func(c,5);
}

void func(int* arr, int size) {
    for(int i=0; i<size; i++)
        display(arr[i]);
}

この例では、配列 b および c が指示付きの初期化子を使用して初期化されているが配列サイズが指定されていない場合、ルールに違反します。

チェック情報

グループ: 初期化
カテゴリ: 必要
AGC カテゴリ: Readability

バージョン履歴

R2024a で導入


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.