Skip to main content

IAR Embedded Workbench for RISC-V 3.40

ITR-store (C++ only)

In this section:
Synopsis

A container's begin() or end() iterator is stored and subsequently used.

Enabled by default

No

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

A container's begin() or end() iterator is stored and subsequently used. If the container is modified, these iterators will become invalidated. This could result in illegal memory access or a crash. Calling begin() and end() as these iterators are needed in loops and comparisons will ensure that only valid iterators are used.

Coding standards

This check does not correspond to any coding standard rules.

Code examples

The following code example fails the check and will give a warning:

#include <vector>

void increment_all(std::vector<int>& v) {
  std::vector<int>::iterator b = v.begin();
  std::vector<int>::iterator e = v.end();
  //Storing these iterators is dangerous and unnecessary
  
  for (std::vector<int>::iterator i = b; i != e; ++i){
    ++(*i);
  }
}

The following code example passes the check and will not give a warning about this issue:

#include <vector>

void increment_all(std::vector<int>& v) {  
  for (std::vector<int>::iterator i = v.begin();
       i != v.end(); ++i){
    ++(*i);  //OK
  }
}