开发者

C Integral Overflow?

开发者 https://www.devze.com 2023-03-18 15:12 出处:网络
have a peek at this. The compiler is complaining that I have an integer overflow, but when I look at the C89 standard\'s rules for integral promotion along with the values in that expression, it seems

have a peek at this. The compiler is complaining that I have an integer overflow, but when I look at the C89 standard's rules for integral promotion along with the values in that expression, it seems to me that there is no overflow.

rutski@imac:~$ cat test.c 
#include <stdio.h>
#include <inttypes.h>

const uint32_t value =
    (0x7F-0x00 + 1) * (256 + 256*256 + 256*256*256) +
    (0xDF-0xC2 + 1) * (256 + 256*256 + 256*256*256);

int
main(void)
{
    printf("value = %"PRIu32"\n", value);
    return 0;
}
rutski@imac:~$ gcc -std=c8开发者_StackOverflow社区9 -pedantic -Wall -Wextra test.c
test.c:5: warning: integer overflow in expression
test.c:6: warning: integer overflow in expression
test.c:6: warning: overflow in constant expression
rutski@imac:~$ ./a.out 
value = 2661195264
rutski@imac:~$ 

Moreover, google confirms that the answer of 2661195264 is the correct value for that expression! (See this link)

So, how is it that the program can produce a correct value when there was integer overflow? And more importantly, how is it that there was integer overflow in that expression to begin with?


(0x7F-0x00 + 1) * (256 + 256*256 + 256*256*256)

has the value 2155905024; the largest representable signed 32-bit int is 2147483647, so you have indeed produced an overflow. It happens to have given you the expected result anyway (you got lucky).

Note that your entire initializer has signed type (type int, specifically), as none of the literals are suffixed. The expression is evaluated as a signed int, then the resulting value is converted to an unsigned integer.


Although your constants are all positive, they are still signed integers. You are overflowing the range of a signed integer. The result is converted to unsigned as the last step when it is assigned to the variable.

The bit patterns for addition, subtraction, and multiplication are identical between signed and unsigned operations. While the compiler is not required to provide the correct answer in this case, it's just the most natural result.

0

精彩评论

暂无评论...
验证码 换一张
取 消