主要内容

Vector declared in loop (EXPENSIVE_VECTOR_IN_LOOP)

R2026b

Allocating and deallocating vectors in a loop results in inefficient code

Since R2026b

Description

This defect occurs when you declare a std::vector inside a loop body. Each loop iteration allocates and deallocates memory for the vector, which degrades performance.

Polyspace® Bug Finder™ flags std::vector declarations inside for, while, and do-while loops, including nested loops. The checker does not flag vectors that are move-initialized inside the loop because moving avoids the allocation overhead.

Risk

Declaring a std::vector inside a loop causes repeated heap allocations and deallocations. These operations are expensive compared to reusing an existing vector:

  • Each iteration invokes the allocator to reserve memory and the destructor to release it.

  • In performance-critical loops, the cumulative cost of these allocations can significantly slow execution.

  • The repeated allocations can also cause heap fragmentation.

Fix

To fix this defect, move the vector declaration outside the loop and call clear() at the beginning of each iteration:

  • Declare the std::vector before the loop.

  • Replace the in-loop declaration with a call to vec.clear() or vec.assign(...) to reset the vector contents without deallocating memory.

Examples

expand all

In this example, the function declares a std::vector inside a range-based for loop. Each iteration allocates and deallocates memory for earnings.

#include <vector>
#include <numeric>

using IncomeCategory = int;

void get_earnings(IncomeCategory cat, std::vector<int>& earnings);

int get_total_earnings(const std::vector<IncomeCategory>& incomes)
{
    int total_income = 0;
    for (const auto& category : incomes) {
        std::vector<int> earnings;  // Defect
        get_earnings(category, earnings);
        total_income += std::accumulate(earnings.begin(), earnings.end(), 0);
    }
    return total_income;
}

Polyspace Bug Finder reports a defect on the declaration of earnings because the vector is declared inside the loop. Each iteration of the loop allocates and deallocates memory for the vector.

Correction — Move Vector Declaration Outside Loop

Move the declaration of earnings before the loop and call clear() at the start of each iteration. The vector reuses its allocated memory across iterations.


#include <vector>
#include <numeric>

using IncomeCategory = int;

void get_earnings(IncomeCategory cat, std::vector<int>& earnings);

int get_total_earnings(const std::vector<IncomeCategory>& incomes)
{
    std::vector<int> earnings;  // No defect
    int total_income = 0;
    for (const auto& category : incomes) {
        earnings.clear();
        get_earnings(category, earnings);
        total_income += std::accumulate(earnings.begin(), earnings.end(), 0);
    }
    return total_income;
}

Result Information

Group: PERFORMANCE
Language: C++
Impact: Medium
Command-Line Syntax: EXPENSIVE_VECTOR_IN_LOOP

Version History

Introduced in R2026b