Main Content
MISRA C:2012 Rule 21.16
The pointer arguments to the Standard Library function memcmp
shall
point to either a pointer type, an essentially signed type, an essentially
unsigned type, an essentially Boolean type or an essentially enum
type
Description
Rule Definition
The pointer arguments to the Standard Library function memcmp
shall
point to either a pointer type, an essentially signed type, an essentially
unsigned type, an essentially Boolean type or an essentially enum
type.
This rule comes from MISRA C™: 2012 Amendment 1.
Rationale
The Standard Library function
memcmp ( lhs, rhs, num );
num
bytes
of the two objects that lhs
and rhs
point
to.Do not use memcmp
for a byte-by-byte comparison
of the following.
Type | Rationale |
---|---|
Structures | If members of a structure have different data types, your compiler
introduces additional padding for data alignment in memory. The content
of these extra padding bytes is meaningless. If you perform a byte-by-byte
comparison of structures with memcmp , you compare
even the meaningless data stored in the padding. You might reach the
false conclusion that two data structures are not equal, even if their
corresponding members have the same value. |
Objects with essentially real floating type and essentially complex floating type | The same floating point value can be stored using different
representations. If you perform a byte-by-byte comparison of two variables
with memcmp , you can reach the false conclusion
that the variables are unequal even when they have the same value.
The reason is that the values are stored using two different representations. |
Essentially char arrays | Essentially char arrays are typically used to store strings.
In strings, the content in bytes after the null terminator is meaningless.
If you perform a byte-by-byte comparison of two strings with memcmp ,
you might reach the false conclusion that two strings are not equal,
even if the bytes before the null terminator store the same value. |
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: Standard libraries |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2017a