File: README-container-inside-loop.md

package info (click to toggle)
clazy 1.17-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 5,248 kB
  • sloc: cpp: 23,552; python: 1,450; xml: 450; sh: 237; makefile: 46
file content (27 lines) | stat: -rw-r--r-- 643 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# container-inside-loop

Finds places defining containers inside loops.
Defining them outside the loop and using `resize(0)` will save memory allocations.

#### Example

    // This will allocate memory at least N times:
    for (int i = 0; i < N; ++i) {
        QVector<int> v;
        (...)
        v.append(bar);
        (...)
    }

    // This will reuse previously allocated memory:
    QVector<int> v;
    for (int i = 0; i < N; ++i) {
        v.resize(0); // resize(0) preserves capacity, unlike QVector::clear()
        (...)
        v.append(bar);
        (...)
    }

#### Supported containers

`QList`, `QVector` and `std::vector`