Inefficient use of for loop
Range-based for loop can perform equivalent iteration more efficiently
Since R2022a
Description
This defect occurs when you use for loops that loop through all
elements of a standard template library (STL) container or a C-style array and these
conditions are true:
The
forloop has only one loop counter variable. For instance, Polyspace® does not flag thisforloop:for(int i=0, j=0; i < 10; ++i, ++j){ //... }The
forloop uses the loop counter variable to access only the elements of the container or the C-style array. For instance, in this code snippet, Polyspace flags the first loop but not the second one.#include <iostream> int myArray[10] void cArray() { //First loop: Loop counter used only to access elements of myArray for (int i = 0; i < 10; ++i) { myArray[i] = 0; } //Second loop: Loop counter assigned to elements of myArray for (int i = 0; i < 10; ++i) { myArray[i] = i; } }The
forloop iterates through the entire array. For example, Polyspace reports a defect on the first loop but not the second one:#include <iostream> int myArray[10] void cArray() { int numbers[] = { 1, 2, 3, 4, 5 }; //First loop: Iterates over entire array for (int i = 0; i < 5; ++i) { std::cout<<number[i]; } //Second loop: Iterates over part of the loop for (int i = 0; i < 4; ++i) { std::cout<<number[i]; } }
Polyspace does not raise an Inefficient use of for loop defect in these scenarios:
The
forloop iterates through a user-defined container. For instance, no defect is reported on thisforloop.Polyspace raises a defect only for C-style arrays and Standard Template Library containers.template<typename T> class customContainer { typedef T* iterator; public: iterator begin(); iterator end(); }; void func() { customContainer<int> myContainer; for(auto it = myContainer.begin(); it != myContainer.end(); it++) { std::cout << *it; } }The
forloop iterates over a C-style array or STL container in a reverse order. For instance, in this code snippet, there is no defect because you cannot use a range-basedforloop to perform an equivalent operation.#include <iostream> #include <vector> #include <cstdint> std::vector<std::uint32_t> myVector(10); void myContainer() { //loop uses reverse iteration for (auto it = myVector.rbegin(); it != myVector.rend(); ++it) { std::cout << *it; } }The
forloop iterates over:Multiple arrays
Elements of a multidimensional array
An array of unknown size
For instance, Polyspace does not flag these
forloops:int myArray[10]; int myOtherArray[10]; int multiArray[10][10]; void cArray() { //loop through multiple array for (int i = 0; i < 10; ++i) { myArray[i] = 0; myOtherArray[i] = 0; } //loop through 2-dimensional array for (int i = 0; i < 10; ++i) { multiArray[i][i] = 0; } } void unknownSize(int someArray[]) { //loop through array of unknown size for (int i = 0; i < 10; ++i) { someArray[i] = 0; } }
Risk
If you loop through all the elements of a container or a C-style array and you use the
loop counter variable to access only the value of the elements, a for
loop is slower, less readable, and more difficult to maintain than an equivalent range-based
for loop.
Fix
Replace the for loop with a range-based for loop,
which makes your code run more efficiently, more readable, and easier to maintain.
Examples
Result Information
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
PREFER_RANGE_BASED_FOR_LOOPS |
| Impact: Low |
PQL Name: std.defects.PREFER_RANGE_BASED_FOR_LOOPS |