Re: [llvm-dev] Confusing UB warning

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [llvm-dev] Confusing UB warning

Victor Leschuk via cfe-dev
Adding cfe-dev. Removing llvm-dev.

On Tue, Apr 18, 2017 at 7:55 AM Manuel Rigger via llvm-dev <[hidden email]> wrote:
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
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
--
~Craig

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Loading...