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

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

If you’re interested in a purely theoretical answer, the C++ standard allows undefined behaviour to “time travel”: [intro.execution]/5: A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. However, if … Read more

Is signed integer overflow still undefined behavior in C++?

is still overflow of these types an undefined behavior? Yes. Per Paragraph 5/4 of the C++11 Standard (regarding any expression in general): If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. […] The fact that a … Read more