Main Content

MISRA C++:2023 Rule 8.9.1

The built-in relational operators >, >=, < and <= shall not be applied to objects of pointer type, except where they point to elements of the same array

Since R2024b

Description

Rule Definition

The built-in relational operators >, >=, < and <= shall not be applied to objects of pointer type, except where they point to elements of the same array.

Rationale

When you compare two pointers to array elements, the result is the positions of the pointers relative to each other. If the pointers are null or point to different arrays, a comparison operation is undefined.

Before you use >, >=, <, or <= between pointers to array elements, check that they are non-null and that they point to the same array.

Polyspace Implementation

Polyspace® flags the use of >, >=, <, or <= operators between pointers to elements of different arrays.

The checker ignores casts when showing the violation on relational operator use with pointers types.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

bool foo(){
	int a[10];
	int b[10];
	return (a<b);
	
}

In this example, Polyspace flags the comparison between a and b, which are elements of different arrays.

Check Information

Group: Expressions
Category: Required

Version History

Introduced in R2024b