Detect accidental elided dimension in C++

Viewed 205

Consider the following snippet:

#include <iostream>
using namespace std;

int a[10][2];
int b[10][2];

int main(){

  //intended
  cout << a[0][0] - b[0][0] << endl;

  //left out dimension by mistake
  cout << a[0] - b[0] << endl;

}

Obviously (or maybe not per comments) the second case is valid pointer arithmetic in both C and C++ but in the code base I am working with it is generally a semantic mistake; a dimension has usually been left out in a nested for loop. Is there any -W flag or static analysis tool that can detect this?

3 Answers
Related