メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

MISRA C:2012 Rule 17.6

The declaration of an array parameter shall not contain the static keyword between the [ ]

説明

ルール定義

The declaration of an array parameter shall not contain the static keyword between the [ ] 1 .

根拠

[] 内で static キーワードを関数の配列パラメーターに対して使用する場合、C99 コンパイラに配列の要素数が最小であると伝えることができます。コンパイラはこの情報を使用して特定のプロセッサに対して効率的なコードを生成できます。ただし、関数呼び出し内で、指定した最小数よりも少ない数を指定する場合、動作は未定義です。

トラブルシューティング

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

すべて展開する

extern int arr1[20];
extern int arr2[10];


unsigned int total (unsigned int n,
                    unsigned int arr[static 20]) { // Non-compliant
 
    unsigned int i;
    unsigned int sum = 0;

    for (i=0U; i < n; i++) {
        sum+= arr[i];
    }

    return sum;
}

void func (void) {
    int res, res2;
    res = total (10U, arr1); //Undefined behavior 
    res2 = total (20U, arr2); 
}

この例では、static キーワードが関数 total の配列パラメーターの [] 内部で使用される場合、ルールに違反します。動作を適切に定義して、total を配列引数で宣言しても、ルール違反は発生します。

チェック情報

グループ: 関数
カテゴリ: Mandatory
AGC カテゴリ: Mandatory

バージョン履歴

R2014b で導入


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.