[llvm-dev] Confusing UB warning

Manuel Rigger via llvm-dev llvm-dev at lists.llvm.org
Tue Apr 18 07:55:23 PDT 2017


Hi,

the C11 standard provides the following example for variable-length arrays
and pointer arithmetics (see ยง6.5.6:10):

int main() {
  int n = 4, m = 3;
  int a[n][m];
  int(*p)[m] = a;
  assert(p == &a[0]);
  p += 1;
  assert(p == &a[1]);
  (*p)[2] = 99;
  assert(a[1][2] == 99);
  n = p - a;
  assert(n == 1);
}

When compiling the program with Clang 3.9, I get the following warning:

warning: subtraction of pointers to type 'int [m]' of zero size has
undefined behavior [-Wpointer-arith]

This is confusing (and probably a bug), since m is not zero and both
pointers refer to the same object. If I define a as int a[n][3] the warning
no longer appears.

- Manuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170418/24092b87/attachment-0001.html>


More information about the llvm-dev mailing list