integer-overflow

Do C99 signed integer types defined in stdint.h exhibit well-defined behaviour in case of an overflow?

佐手、 提交于 2019-12-18 04:44:11
问题 All operations on "standard" signed integer types in C (short, int, long, etc) exhibit undefined behaviour if they yield a result outside of the [TYPE_MIN, TYPE_MAX] interval (where TYPE_MIN, TYPE_MAX are the minimum and the maximum integer value respectively. that can be stored by the specific integer type. According to the C99 standard, however, all intN_t types are required to have a two's complement representation: 7.8.11.1 Exact-width integer types 1. The typedef name intN_t designates a

Convert INT_MAX to float and then back to integer.

冷暖自知 提交于 2019-12-17 19:39:41
问题 In C programming, I find a weird problem, which counters my intuition. When I declare a integer as the INT_MAX ( 2147483647 , defined in the limits.h) and implicitly convert it to a float value, it works fine, i.e., the float value is same with the maximum integer. And then, I convert the float back to an integer, something interesting happens. The new integer becomes the minimum integer ( -2147483648 ). The source codes look as below: int a = INT_MAX; float b = a; // b is correct int a_new =

At what point in the loop does integer overflow become undefined behavior?

≯℡__Kan透↙ 提交于 2019-12-17 17:39:32
问题 This is an example to illustrate my question which involves some much more complicated code that I can't post here. #include <stdio.h> int main() { int a = 0; for (int i = 0; i < 3; i++) { printf("Hello\n"); a = a + 1000000000; } } This program contains undefined behavior on my platform because a will overflow on the 3rd loop. Does that make the whole program have undefined behavior, or only after the overflow actually happens ? Could the compiler potentially work out that a will overflow so

At what point in the loop does integer overflow become undefined behavior?

◇◆丶佛笑我妖孽 提交于 2019-12-17 17:39:07
问题 This is an example to illustrate my question which involves some much more complicated code that I can't post here. #include <stdio.h> int main() { int a = 0; for (int i = 0; i < 3; i++) { printf("Hello\n"); a = a + 1000000000; } } This program contains undefined behavior on my platform because a will overflow on the 3rd loop. Does that make the whole program have undefined behavior, or only after the overflow actually happens ? Could the compiler potentially work out that a will overflow so

Why don't languages raise errors on integer overflow by default?

烈酒焚心 提交于 2019-12-17 03:22:26
问题 In several modern programming languages (including C++, Java, and C#), the language allows integer overflow to occur at runtime without raising any kind of error condition. For example, consider this (contrived) C# method, which does not account for the possibility of overflow/underflow. (For brevity, the method also doesn't handle the case where the specified list is a null reference.) //Returns the sum of the values in the specified list. private static int sumList(List<int> list) { int sum

Gini coefficient, ineq package in R and integer overflow

ぐ巨炮叔叔 提交于 2019-12-14 03:06:35
问题 I want to calculate the Gini coefficient for each column in a 2090 x 25 dataframe. I am using the Gini function in the ineq package and the following code: gini <- sapply(mydata, function(x) ineq(x,type="Gini")). This produces results that look valid but also the following warning message: Warning messages: 1: In n * sum(x) : NAs produced by integer overflow 2: In sum(x * 1:n) : Integer overflow - use sum(as.numeric(.)) 3: In n * sum(x) : NAs produced by integer overflow To overcome the

How does one safely static_cast between unsigned int and int?

我是研究僧i 提交于 2019-12-13 11:56:23
问题 I have an 8-character string representing a hexadecimal number and I need to convert it to an int . This conversion has to preserve the bit pattern for strings "80000000" and higher, i.e., those numbers should come out negative. Unfortunately, the naive solution: int hex_str_to_int(const string hexStr) { stringstream strm; strm << hex << hexStr; unsigned int val = 0; strm >> val; return static_cast<int>(val); } doesn't work for my compiler if val > MAX_INT (the returned value is 0). Changing

Implementing / enforcing wraparound arithmetic in C [closed]

雨燕双飞 提交于 2019-12-13 09:48:55
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . The C standard says that overflow in arithmetic is undefined. I would like to know how to implement wraparound arithmetic in a performance-friendly way. This means that overflow checking solutions like presented here are not an option (as they slow the operation by about an order

How many bits are available for JS-style “integer” math now?

时间秒杀一切 提交于 2019-12-13 07:33:42
问题 While solving my previous question I faced even more funny thing. Try integer math (yes, I know they aren't "too integer" inside) to see how much bits are available: var n = 0xffffffff; // loop over 1P: for(var i=1; i<=1024*1024*1024*1024; i*=16) { var v=(n*i).toString(16); console.log('i='+i+'; v='+v+' ('+v.length*4+')'); } // Output: // i=1; v=ffffffff (32) // i=16; v=ffffffff0 (36) // i=256; v=ffffffff00 (40) // i=4096; v=ffffffff000 (44) // i=65536; v=ffffffff0000 (48) // i=1048576; v

Different Int values for the same value?

[亡魂溺海] 提交于 2019-12-13 03:13:24
问题 I ran into an integer overflow problem, which I managed to solve, but only by trial and error. Since it's an integer overflow problem, I've wrote some code to print out the buffer. The beginning of the buffer is the address where array[0] is stored. Then, I started to pass MAX_INT and MIN_INT values to the program. I've noticed that when I passed MIN_INT value to argv[1], it overwrote the begining of the buffer. so I passed MIN_INT+1 value, and noticed that it overwrote the second address of