[cfe-dev] [llvm-dev] Confusing UB warning

Craig Topper via cfe-dev cfe-dev at lists.llvm.org
Tue Apr 18 07:58:55 PDT 2017


Adding cfe-dev. Removing llvm-dev.

On Tue, Apr 18, 2017 at 7:55 AM Manuel Rigger via llvm-dev <
llvm-dev at lists.llvm.org> 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
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-- 
~Craig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170418/4c2a7e4b/attachment.html>


More information about the cfe-dev mailing list