このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
MISRA C:2023 Rule 12.5
The sizeof
operator shall not have an operand which is a function parameter declared as “array of type”
R2024a 以降
説明
ルール定義
The sizeof
operator shall not have an operand which is a function parameter declared as “array of type”. 1
This rule comes from MISRA C™:2012 Amendment 1.
根拠
配列に作用する sizeof
演算子は、通常、配列のサイズをバイト単位で返します。たとえば、次のコードの sizeof(arr)
は、arr
のサイズをバイト単位で返します。
int32_t arr[4]; size_t numberOfElements = sizeof (arr) / sizeof(arr[0]);
ただし、その配列が関数パラメーターの場合、ポインターに退化します。この配列に作用する sizeof
演算子は、配列のサイズではなく、対応するポインターのサイズを返します。
関数パラメーターとして受け取る配列に sizeof
演算子を使用すると、通常、意図しないプログラミング エラーが発生します。
トラブルシューティング
ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。
例
チェック情報
グループ: 式 |
カテゴリ: Mandatory |
AGC カテゴリ: Mandatory |
バージョン履歴
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.