Ad Space — Top Banner

EIntOverflow

Delphi Programming Language

Severity: Moderate

What Does This Error Mean?

EIntOverflow is raised when an integer arithmetic operation produces a result that is too large to fit in the integer type being used. This only happens when overflow checking is enabled with {$OVERFLOWCHECKS ON}. Without that directive, integer overflow silently wraps around, which is often a harder bug to detect.

Affected Models

  • Delphi 10.4 Sydney
  • Delphi 11 Alexandria
  • Delphi 12 Athens
  • Embarcadero RAD Studio
  • Free Pascal / Lazarus

Common Causes

  • Multiplying two large integers whose product exceeds MaxInt (2,147,483,647 for 32-bit Integer)
  • Adding values in a loop without checking whether the accumulator is approaching the type limit
  • Performing arithmetic on Byte, SmallInt, or ShortInt types where the result exceeds the narrow type range
  • Computing factorials, powers, or other rapidly growing sequences without using Int64 or larger types
  • Converting a large floating-point value to an integer type that cannot hold the value

How to Fix It

  1. Run the program in the debugger. When EIntOverflow fires, note the line and the values involved. Determine the maximum possible result of the calculation.

    Integer (32-bit signed) holds -2,147,483,648 to 2,147,483,647. Int64 (64-bit signed) holds approximately -9.2 quintillion to 9.2 quintillion.

  2. Change the variable type to a wider type. Replace Integer with Int64, or Cardinal with UInt64, for values that can grow large.

    Delphi promotes arithmetic automatically when both operands are Int64. If only one operand is Int64, cast the other: Int64(A) * B.

  3. For intermediate calculations, cast to Int64 before the multiplication: Result := Int64(A) * Int64(B). This prevents overflow in the intermediate result even if the final result fits in Integer.

    A * B where A and B are both Integer is computed as Integer, even if you assign the result to Int64 — the overflow happens before the assignment.

  4. For accumulating loops, check the value before adding: if Accumulator > MaxInt - Addition then raise an error or use Int64 for the accumulator.

    Pre-checking prevents overflow in loops that process large datasets.

  5. Review the algorithm. If values naturally grow very large, consider using Double or Extended floating-point instead of integers, accepting some precision loss in exchange for range.

    Double can represent values up to about 1.8 x 10^308, far exceeding any integer type.

When to Call a Professional

EIntOverflow means the calculation produced a mathematically correct result that cannot be stored in the chosen type. The fix is to use a wider type (Int64 or UInt64 instead of Integer) or to restructure the calculation to avoid the overflow. Enabling {$OVERFLOWCHECKS ON} in all builds is recommended — silent overflow wrapping produces corrupt data that is much harder to debug.

Frequently Asked Questions

What happens if I remove {$OVERFLOWCHECKS ON} — does the overflow go away?

No — the overflow still happens, but silently. Without overflow checking, Delphi wraps the result around (e.g., MaxInt + 1 becomes -2147483648). This produces wrong calculations without any error, which is usually a worse outcome than an exception. Keep overflow checks on during development and fix the root cause.

Is EIntOverflow the same as EIntError?

EIntError is the base class for integer runtime errors. EIntOverflow (overflow) and EDivByZero (division by zero) are both subclasses of EIntError. You can catch EIntError to handle all integer arithmetic errors in one place, or catch EIntOverflow and EDivByZero separately for more precise handling.

Why does my code overflow on 32-bit but not 64-bit?

On 64-bit Windows, Delphi's NativeInt and NativeUInt types are 64 bits wide instead of 32 bits. Code using these types benefits from the larger range automatically on 64-bit. However, variables explicitly declared as Integer are always 32-bit regardless of platform — they can still overflow on both platforms.