I have this simple test code that creates a vector of 2 elements and prints it’s size, then prints first element:
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<double> arr {0, 0.1};
cout << arr.size() << endl;
cout << arr[0] << endl;
}
The code compiles and runs fine and outputs 2 then 0 as it should.
However when I run cppcheck on this file it reports this:
error: Out of bounds access in expression 'arr[0]' because 'arr' is empty. [containerOutOfBounds]
cout << arr[0] << endl;
^
But arr
is clearly not empty!
Is this a false positive or am I missing something?
Cppcheck version 2.12