MISRA C++:2008 Rule 5-0-15
Array indexing shall be the only form of pointer arithmetic
Description
Rule Definition
Array indexing shall be the only form of pointer arithmetic.
Rationale
You can traverse an array in two ways:
Increment or decrement an array index, and then use the array index to access an element.
Increment or decrement a pointer to the array, and then dereference the pointer.
The first method is clearer and less error-prone. All other forms of explicit pointer arithmetic introduce the risk of accessing unintended memory locations.
As an exception, incrementing or decrementing pointer based iterators is compliant with this rule.
Polyspace Implementation
The checker flags:
Arithmetic operations on all pointers, for instance
p+I
,I+p
andp-I
, wherep
is a pointer andI
an integer..Array indexing on nonarray pointers.
Polyspace® does not flag incrementing or decrementing pointer based iterators, including these standard iterator types:
iterator
cont_iterator
reverse_iterator
const_reverse_iterator
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
Check Information
Group: Expressions |
Category: Required |
Version History
Introduced in R2013b