What does the warning integer overflow in expression mean?
It means that the operation goes outside of the valid range of the particular type of integer. For example, if you have an integer of type unsigned char
, then the range is 0..255
which means that doing 2 - 4
is an "overflow" because it goes negative, and, of course, 253 + 5
is also an "overflow" because it goes beyond the max of 255
.
Why am I getting it?
For one, the &type - 1
operation could overflow by making the pointer "negative". Pointers are, in general, just unsigned integers that are interpreted as address values, and therefore, producing a negative pointer is an overflow error.
Another issue is that if you are in a 64bit platform, then pointers are 8 bytes long, while integers (int) are typically 4 bytes long. I think that by default, in C, the operations are done in the default integer type int
unless it is explicitely made otherwise. But I could be mistaken, I'm not super well versed in the soft typing rules of C. But when I generate the assembly listing for your program (after a few tricks to prevent optimizations), this is confirmed by the following assembly code:
leaq -8(%rbp), %rcx // &x -> rcx (64bit reg.)
movq %rcx, %rsi // rcx -> rsi (save it)
addq $-4, %rcx // substracts 4 from &x
subq %rcx, %rsi // does (&x - (&x - 1))
movl %esi, %r8d …