MISRA C:2023 Rule 12.5
The sizeof
operator shall not have an operand which is a function
parameter declared as “array of type”
Since R2024a
Description
Rule Definition
The sizeof
operator shall not have an operand which is a
function parameter declared as “array of type”.
This rule comes from MISRA C™: 2012 Amendment 1.
Rationale
The sizeof
operator acting on an array normally
returns the array size in bytes. For instance, in the following code, sizeof(arr)
returns
the size of arr
in bytes.
int32_t arr[4]; size_t numberOfElements = sizeof (arr) / sizeof(arr[0]);
However, when the array is a function parameter, it degenerates
to a pointer. The sizeof
operator acting on the
array returns the corresponding pointer size and not the array size.
The use of sizeof
operator on an array that
is a function parameter typically indicates an unintended programming
error.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Expressions |
Category: Mandatory |
AGC Category: Mandatory |
Version History
Introduced in R2024a