Sign in Join

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

Mailing lists / fpc-pascal / 2018-09

Hint converting to int64

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Santiago A.

Hello:
FPC: 3.0.4 (Realease from Lazarus 1.8.4 SVN: 57972) OS: Windows7 32bits / Linux 64Bits

I have this code and I get a hint

---------- var  Entity:Longword;FullParagraph:string; pIni:Integer;
begin .... Entity:=Entity*10+ord(FullParagraph[pIni])-48; // <=== Hint
.... end; -----------

Hint: Converting the operands to "Int64" before doing the add could prevent overflow errors.
I can't see why it mentions int64, there are integer and longword variables, but no Int64 one.

I have replaced it by

Entity:=Entity*10+LongWord(ord(FullParagraph[pIni]))-48;

That is, I have casted the result of "ord()" to longword. But I get the same hint.
What should I do to remove the hint?

Saludos Santiago A.

Reply by email

Mark Morgan Lloyd
to Santiago A.
Santiago A. wrote 9 lines
Hello:FPC: 3.0.4 (Realease from Lazarus 1.8.4 SVN: 57972)OS: Windows7 32bits / Linux 64Bits I have this code and I get a hint ---------- var  Entity:Longword;FullParagraph:string; pIni:Integer; begin .... Entity:=Entity*10+ord(FullParagraph[pIni])-48; // <=== Hint .... end; ----------- Hint: Converting the operands to "Int64" before doing the add could prevent overflow errors.I can't see why it mentions int64, there are integer and longword variables, but no Int64 one.

There is you know.

https://www.freepascal.org/docs-html/current/ref/refsu4.html#x26-250003.1.1

I have replaced it by Entity:=Entity*10+LongWord(ord(FullParagraph[pIni]))-48; That is, I have casted the result of "ord()" to longword. But I get the same hint.What should I do to remove the hint?
Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Santiago A.
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 15 lines
On 11/09/18 10:15, Santiago A. wrote: > Hello:FPC: 3.0.4 (Realease from Lazarus 1.8.4 SVN: 57972)OS: Windows7 > 32bits / Linux 64Bits > I have this code and I get a hint > ---------- var  Entity:Longword;FullParagraph:string; pIni:Integer; > begin .... Entity:=Entity*10+ord(FullParagraph[pIni])-48; // <=== > Hint .... end; ----------- > Hint: Converting the operands to "Int64" before doing the add could > prevent overflow errors.I can't see why it mentions int64, there are > integer and longword variables, but no Int64 one. There is you know. https://www.freepascal.org/docs-html/current/ref/refsu4.html#x26-250003.1.1
----------
var
   Entity:Longword;
   FullParagraph:string;
   pIni:Integer;
begin
   ....
   Entity:=Entity*10+ord(FullParagraph[pIni])-48; // <=== Hint
   ....
end;
  -----------
Do you mean that Entity is promoted to int64 in a 32bits system?
Saludos Santiago A.

Reply by email

Mark Morgan Lloyd
to Santiago A.
Santiago A. wrote 18 lines
El 11/09/18 a las 12:30, Mark Morgan Lloyd escribió:> On 11/09/18 10:15, Santiago A. wrote:>> Hello:FPC: 3.0.4 (Realease from Lazarus 1.8.4 SVN: 57972)OS: Windows7 >> 32bits / Linux 64Bits>> I have this code and I get a hint>> ---------- var  Entity:Longword;FullParagraph:string; pIni:Integer; >> begin .... Entity:=Entity*10+ord(FullParagraph[pIni])-48; // <=== >> Hint .... end; ----------->> Hint: Converting the operands to "Int64" before doing the add could >> prevent overflow errors.I can't see why it mentions int64, there are >> integer and longword variables, but no Int64 one.>> There is you know.>> https://www.freepascal.org/docs-html/current/ref/refsu4.html#x26-250003.1.1 > ----------var   Entity:Longword;   FullParagraph:string; pIni:Integer;begin   .... Entity:=Entity*10+ord(FullParagraph[pIni])-48; // <=== Hint   ....end;  -----------Do you mean that Entity is promoted to int64 in a 32bits system?

The operands Entity*10 and 10+ord(FullParagraph[pIni])-48 are both 32 bits, but the addition could overflow. The compiler writer's trying to be nice to you, quit kvetching :-)

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Martok
to Santiago A.: Hello: FPC: 3.0.4 (Realease from Lazarus 1.8.4 SVN: 57972) OS: Windows7 32bits / Linux…
Santiago A. wrote
---------- var Entity:Longword;FullParagraph:string; pIni:Integer; begin .... Entity:=Entity*10+ord(FullParagraph[pIni])-48; // <=== Hint .... end; -----------

Compiling with -vp shows that the result of the subtraction is a signed Longint. That makes this an expression mixing Longword and Longint, which is always computed in Int64 and because of that causes this hint.

The thing I'm missing is the "Mixing signed expressions and longwords gives a 64bit result" hint. Is that because something detects that this was an internal typeconvn?

Regards, Martok

Reply by email