integer-overflow

g++ strict overflow, optimization, and warnings

為{幸葍}努か 提交于 2019-12-01 06:20:17
问题 When compiling the following with the strict overflow flag, it tells me, on the 2nd test that r may not be what I think it could be: int32_t r(my_rand()); if(r < 0) { r = -r; if(r < 0) { // <-- error on this line r = 0; } } The error is: /build/buildd/libqtcassandra-0.5.5/tests/cassandra_value.cpp: In function 'int main(int, char**)': /build/buildd/libqtcassandra-0.5.5/tests/cassandra_value.cpp:2341:13: error: assuming signed overflow does not occur when simplifying conditional to constant [

Is it undefined behavior if the intermediate result of an expression overflows?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 06:08:11
This question is a result of another SO question . Example Code #include <iostream> int main() { unsigned long b = 35000000; int i = 100; int j = 30000000; unsigned long n = ( i * j ) / b; // #1 unsigned long m = ( 100 * 30000000 ) / b; // #2 std::cout << n << std::endl; std::cout << m << std::endl; } Output 85 85 Compiling this code with g++ -std=c++11 -Wall -pedantic -O0 -Wextra gives the following warning: 9:28: warning: integer overflow in expression [-Woverflow] Questions Am I correct in thinking that #1 and #2 invoke undefined behavior because the intermediate result 100 * 30000000 does

Is it undefined behavior if the intermediate result of an expression overflows?

懵懂的女人 提交于 2019-12-01 03:39:31
问题 This question is a result of another SO question. Example Code #include <iostream> int main() { unsigned long b = 35000000; int i = 100; int j = 30000000; unsigned long n = ( i * j ) / b; // #1 unsigned long m = ( 100 * 30000000 ) / b; // #2 std::cout << n << std::endl; std::cout << m << std::endl; } Output 85 85 Compiling this code with g++ -std=c++11 -Wall -pedantic -O0 -Wextra gives the following warning: 9:28: warning: integer overflow in expression [-Woverflow] Questions Am I correct in

executing a process with argc=0

痞子三分冷 提交于 2019-12-01 03:28:26
Is it possible to execute a process whose argc = 0? I need to execute a program but it is extremely important for its argc to be equal to 0. Is there a way to do that? I tried to put 2^32 arguments in the command line so that it appears as if argc = 0 but there is a maximum limit to the number of arguments. You can write a program that calls exec directly; that allows you to specify the command-line arguments (including the program name) and lack thereof. You may use linux system call execve() . int execve(const char *filename, char *const argv[], char *const envp[]); You may pass the filename

Warning : overflow in implicit constant conversion

怎甘沉沦 提交于 2019-12-01 02:35:53
In the following program, the line 5 does give overflow warning as expected, but surprisingly the line 4 doesn't give any warning in GCC: http://www.ideone.com/U0BXn int main() { int i = 256; char c1 = i; //line 4 char c2 = 256; //line 5 return 0; } I was thinking both lines should give overflow warning. Or is there something I'm missing? The topic which led me to do this experiment is this: typedef type checking? There I said the following(which I deleted from my answer, because when I run it, it didn't show up as I had expected): //However, you'll get warning for this case: typedef int T1;

Unsigned arithmetic and integer overflow

拥有回忆 提交于 2019-12-01 01:08:16
I am trying to understand arithmetic overflow. Suppose I have the following, unsigned long long x; unsigned int y, z; x = y*z; y*z can lead to an integer overflow. Does casting one of the operands to an unsigned long long alleviate this issue. What is the expected result of the multiplication of a 64-bit operand with a 32-bit operand? unsigned long long x; unsigned int y, z; x = y*z; The evaluation of the expression y*z is not affected by the context in which it appears. It multiplies two unsigned int values, yielding an unsigned int result. If the mathematical result cannot be represented as

executing a process with argc=0

别来无恙 提交于 2019-11-30 23:45:08
问题 Is it possible to execute a process whose argc = 0? I need to execute a program but it is extremely important for its argc to be equal to 0. Is there a way to do that? I tried to put 2^32 arguments in the command line so that it appears as if argc = 0 but there is a maximum limit to the number of arguments. 回答1: You can write a program that calls exec directly; that allows you to specify the command-line arguments (including the program name) and lack thereof. 回答2: You may use linux system

how to calculate (a times b) divided by c only using 32-bit integer types even if a times b would not fit such a type

安稳与你 提交于 2019-11-30 22:29:49
Consider the following as a reference implementation: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint64_t x = a; x = x * b; x = x / c; return x; } I am interested in an implementation (in C or pseudocode) that does not require a 64-bit integer type. I started sketching an implementation that outlines like this: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint32_t d1, d2, d1d2; d1 = (1 << 10); d2 = (1 << 10); d1d2 = (1 << 20); /* d1 * d2 */ return ((a / d1) * (b /d2)) / (c / d1d2); } But the difficulty is to pick

What happens when a char is assigned a value too large to fit in a byte?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 22:12:08
Let's say I have a char pointer: char * cp; int val2 = 2; cp = &val2; *cp = 1234; What will happen since the value 1234 is too large to fit in 1 byte? Will it just overflow and cause incorrect values to be stored, or will it somehow store the correct value across several bytes? In *cp = 1234; , *cp refers to just one byte, the first (lowest-addressed) byte of val2 . In an assignment, the value of the right-hand side is converted to the type of the left side. So, 1234 will be converted to char . There are two complications here. One, in most C implementations, 1234 is too big to fit into a char

C++ integer overflow

浪子不回头ぞ 提交于 2019-11-30 20:36:21
问题 I'm just starting to teach myself C++, and have begun learning about integer overflow. Out of curiosity I wrote some tests just to see what occurs with certain integer values. Here's my program: #include <iostream> int main() { int x(0); std::cout << x << std::endl; x = x + 2147483647; std::cout << x << std::endl; x = x + 1; std::cout << x << std::endl; std::cout << std::endl; unsigned int y(0); std::cout << y << std::endl; y = y + 4294967295; std::cout << y << std::endl; y = y + 1; std::cout