Lucky hour! Grab a free trial license — offer ends in 00:59:58. Claim now
This diagnostic rule is based on the software development guidelines developed by MISRA (Motor Industry Software Reliability Association).
The C/C++ standard specifies (C11 § 6.5.8 paragraph 5; C++17 § 8.5.9 paragraph 3) that applying the operator '-', '>', '>=', '<', or '<=' to two pointers that do not point to the elements of the same array is undefined/unspecified behavior. Therefore, if two pointers point to different array objects, then these objects must be elements of the same array to be comparable.
Example of non-compliant code:
int arr1[10];
int arr2[10];
int *pArr1 = arr1;
if (pArr1 < arr2)
{
....
}
The following code is also non-compliant:
int arr1[10];
int arr2[10];
int *pArr1 = &arr1[1];
int *pArr2 = &arr2[1];
int len = pArr1 - pArr2;
To learn more about why pointer comparisons may lead to errors, see the article: "Pointers are more abstract than you might expect in C".
This diagnostic is classified as:
|
Was this page helpful?
Your message has been sent. We will email you at
If you do not see the email in your inbox, please check if it is filtered to one of the following folders: