Jump to content
Fritzew

Overflow in Compile

Recommended Posts

Delphi 10.3.2

This code will not compile!  

Stripped down from a TestSuite for our codebase. 

Can't believe it.

 

 

 

program Project17;
{$APPTYPE CONSOLE}
{$R *.res}
uses
  System.SysUtils;
Procedure Test;
var
  int32max: Int64;
begin
  int32max := MaxInt + 1; //Error here
  writeLn(Format('Integer  %20d', [int32max]));
  writeLn(Format('Integer  %20u', [-int32max]));
end;
begin
  try
    Test;
    readln;
  except
    on E: Exception do
      writeLn(E.ClassName, ': ', E.Message);
  end;
end.

 

Error (in German)

[dcc32 Fehler] Project17.dpr(13): E2099 Überlauf bei Konvertierung oder arithmetischer Operation

 

Edited by Fritzew

Share this post


Link to post

Working as designed.

 

Compiler evaluates mathematical operation first and only then it will do assignment. Since your operation works on two integers, the result will also be integer - and it will overflow at compile time. 

 

You need to typecast at least one of the operators to Int64 to widen operation result.

  • Like 4

Share this post


Link to post

You are right and I :classic_ohmy:
I need a break for sure.....
 

int32max := Int64(MaxInt) + 1; // will work

Shame on me

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×