Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2015-02

Implicit Type Conversion when running with range and overflow checking

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Louis Salkind

Pardon the stupid question(s)... I was surprised by the behavior of the following code when range checking and overflow checking was enabled on win32:

procedure test_rangeoverflow;
var
   a, b: BYTE;
   i: Integer;
begin
   a := 128;
   b := 129;
   i := a + b;   // no errors, i is 257; a and b have been promoted to
integers
                 // but shouldn't there be a range check, as a+b has
overflowed a byte and should not be promoted?
   i := BYTE(a + b);    // no errors, and i is 1.  Even if a+b is
promoted to integer,
                        // shouldn't there be range check when coercing
and integer of 257 to byte?
   a := a + b;    // gives range check when storing result, but not an
overflow error first
end;

Comments? I'm guessing this is either specified in the Pascal standard, or fpc is emulating Delphi behavior. Nevertheless, the result seemed non-intuitive enough to me that I wanted to confirm this was the intended behavior.

Reply by email

Nikolay Nikolov
to Louis Salkind
Louis Salkind wrote 15 lines
Pardon the stupid question(s)... I was surprised by the behavior of the following code when range checking and overflow checking was enabled on win32: procedure test_rangeoverflow; var a, b: BYTE; i: Integer; begin a := 128; b := 129; i := a + b; // no errors, i is 257; a and b have been promoted to integers // but shouldn't there be a range check, as a+b has overflowed a byte and should not be promoted?

No, because both are implicitly converted to the natural integer type for the platform (32-bit on 32-bit platforms, 64-bit on 64-bit platforms, 16-bit on i8086), then the addition is done using that type.

i := BYTE(a + b); // no errors, and i is 1. Even if a+b is promoted to integer, // shouldn't there be range check when coercing and integer of 257 to byte?

No, because a typecast to a different integer type explicitly disables range checking. IOW, it means "I know what I'm doing, truncate this to a byte and just ignore the higher bits." If you want range checking, don't use a typecast.

a := a + b; // gives range check when storing result, but not an overflow error first

An overflow error is like a range check error, but is produced when you exceed the natural integer size of the processor. So, on a 32-bit platform, adding two 32-bit integers may produce an overflow error, while adding 16-bit integers may produce a range check error. AFAIK, this distinction is done for BP7 and Delphi compatibility reasons, but anyhow, you, as a programmer, can think of them as being the same thing.

Show quoted text 6 lines
end; Comments? I'm guessing this is either specified in the Pascal standard, or fpc is emulating Delphi behavior. Nevertheless, the result seemed non-intuitive enough to me that I wanted to confirm this was the intended behavior.

I'm not familiar with Pascal standards (although I thing at least Standard Pascal has only one integer type, so the standard says nothing about mixing different integer types), but this is the BP7 and Delphi behavior.

Nikolay

Reply by email

Marco van de Voort
to Nikolay Nikolov

In our previous episode, Nikolay Nikolov said:

Show quoted text 8 lines
> Comments? I'm guessing this is either specified in the Pascal > standard, or fpc is emulating Delphi behavior. Nevertheless, the > result seemed non-intuitive enough to me that I wanted to confirm this > was the intended behavior. I'm not familiar with Pascal standards (although I thing at least Standard Pascal has only one integer type, so the standard says nothing about mixing different integer types), but this is the BP7 and Delphi behavior.

Lower types are made by subranges. (BYTE = 0..255;), it is types larger than integer that set TP/FPC/Delphi apart in that sense.

Reply by email