Sign in Join

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

Mailing lists / fpc-pascal / 2016-02

Format and Cardinal

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Felipe Monteiro de Carvalho

Hello,

Why does this trigger an range check exception:

var
  lNum: Cardinal;
  lStr: string;
begin
  lNum := High(Cardinal);
  lStr := Format('Num=%u', [lNum]);

Format doesn't support integer numbers above High(Integer) ?

I really need to use IntToStr?

thanks,

Felipe Monteiro de Carvalho
Jonas Maebe
to Felipe Monteiro de Carvalho

Felipe Monteiro de Carvalho wrote on Wed, 10 Feb 2016:

Show quoted text 10 lines
Why does this trigger an range check exception: var lNum: Cardinal; lStr: string; begin lNum := High(Cardinal); lStr := Format('Num=%u', [lNum]); Format doesn't support integer numbers above High(Integer) ?

Array of const supports longint, int64 and qword, but no cardinal (Delphi compatibility, and adding it would also mean that all callee side code for array of const would have to be adapted to deal with vtCardinal). Cardinal is passed as longint and not as int64, again for Delphi compatibility and because existing code may behave unexpectedly should this be changed.

In Delphi modes, for Delphi compatibility, we don't perform range checking when passing a cardinal to an array of const. In FPC modes, we do.

I really need to use IntToStr?

That, or typecast the cardinal either to longint or int64, or locally disable range checking.

Jonas

Jonas Maebe
to Felipe Monteiro de Carvalho

Felipe Monteiro de Carvalho wrote on Wed, 10 Feb 2016:

lol, thanks for the comprehensive reply, if no one objects I will add a note about this in http://www.freepascal.org/docs-html/rtl/sysutils/format.html

That is the wrong place. It has nothing to do with format, it's about the "array of const" construct and applies to any routine accepting such parameters. I just noticed there's an error in the array of const documentation though: at the bottom of
http://www.freepascal.org/docs-html/ref/refsu70.html it says that there is no support for qword in array of const, but that's wrong. It's cardinal that is not supported.

Jonas