Compiler optimizations may cause integer overflow. Is that okay?

As Miles hinted: The C++ code text is bound by the rules of the C++ language (integer overflow = bad), but the compiler is only bound by the rules of the cpu (overflow=ok). It is allowed to make optimizations that the code isn’t allowed to. But don’t take this as an excuse to get lazy. … Read more

Can argc overflow?

According to the standard So, from your quote: argv[argc] is required to be a null pointer Therefore, argc cannot overflow, because then the above statement would not be true. In practice In practice, the total size of the arguments passed to a program is limited. On my Linux/x64 system: $ getconf ARG_MAX 2097152 Therefore, the … Read more

What happens when auto_increment on integer column reaches the max_value in databases?

Jim Martin’s comment from §3.6.9. “Using AUTO_INCREMENT” of the MySQL documentation: Just in case there’s any question, the AUTO_INCREMENT field /DOES NOT WRAP/. Once you hit the limit for the field size, INSERTs generate an error. (As per Jeremy Cole) A quick test with MySQL 5.1.45 results in an error of: ERROR 1467 (HY000): Failed … Read more

No overflow exception for int in C#?

C# integer operations don’t throw exceptions upon overflow by default. You can achieve that via the project settings, or by making the calculation checked: int result = checked(largeInt + otherLargeInt); Now the operation will throw. The opposite is unchecked, which makes any operation explicitly unchecked. Obviously, this only makes sense when you’ve got checked operations … Read more

If a 32-bit integer overflows, can we use a 40-bit structure instead of a 64-bit long one?

Yes, but… It is certainly possible, but it is usually nonsensical (for any program that doesn’t use billions of these numbers): #include <stdint.h> // don’t want to rely on something like long long struct bad_idea { uint64_t var : 40; }; Here, var will indeed have a width of 40 bits at the expense of … Read more

Program behaving strangely on online IDEs

I’m going to assume that the online compilers use GCC or compatible compiler. Of course, any other compiler is also allowed to do the same optimization, but GCC documentation explains well what it does: -faggressive-loop-optimizations This option tells the loop optimizer to use language constraints to derive bounds for the number of iterations of a … Read more