$Q or $OV or $OVERFLOWCHECKS: Overflow checking

[next] [prev] [prev-tail] [tail] [up]

#### 1.2.64 $Q or $OV or $OVERFLOWCHECKS: Overflow checking

The {$Q+} or {$OV+} (MACPAS mode only) or {$OVERFLOWCHECKS ON} directive turns on integer overflow checking. This means that the compiler inserts code to check for overflow when doing computations with integers. When an overflow occurs, the run-time library will generate a run-time error 215: it prints a message Overflow at xxx, and exits the program with exit code 215.

Remark Overflow checking behavior is not the same as in Turbo Pascal since all arithmetic operations are done via 32-bit or 64-bit values. Furthermore, the Inc() and Dec() standard system procedures are checked for overflow in Free Pascal, while in Turbo Pascal they are not.

Using the {$Q-} switch (or the {$OV-} switch in MACPAS mode) switches off the overflow checking code generation.

The generation of overflow checking code can also be controlled using the -Co command line compiler option (see the User’s Guide).

In Delphi, overflow checking is only switchable at the procedure level. In Free Pascal, the {$Q} directive can be used at the expression level.

Remark Overflow checking and range checking (see the next section) are basically the same error, except that one is detected by checking the overflow flag of the cpu and the other by explicitly comparing ranges.

On 64 bit cpus, integer arithmetic is performed using 64 bit integers Because by convention in Pascal expressions are evaluated using the native signed integer type, on 64 bit cpus integer arithmetic are performed using 64 bit integers.

As a result, overflows cannot occur there when performing 32 bit arithmetic, instead a range error will occur when assigning the result to a 32 bit variable.

[next] [prev] [prev-tail] [front] [up]