Main Content

MISRA C++:2008 Rule 5-0-18

>, >=, <, <= shall not be applied to objects of pointer type, except where they point to the same array

Description

Rule Definition

>, >=, <, <= shall not be applied to objects of pointer type, except where they point to 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.

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); //Noncompliant
	
}

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 R2013b

expand all