Sign in Join

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

Mailing lists / fpc-pascal / 2010-01

Stupid question: is this safe?

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Bart

Hi,

Is it safe to do the following?

var
  W: Word;
  U: UInt64;
{$R-} //range checkin off
begin
  U := $0000000100000000;
  W := U; // or W := Word(U);
end.

I know that the value that i assigned to W is "wrong" (and I can handle that), but does it trash memory or cause other serious problems?

Context: I have to deal with string input of arbitrary length (only digits) that I have to convert to a Value of type Word. For this I use Val().

Val('$0000000100000000', W, Err) gives W = 0; Err = 0 (indicating
there was no error);

I do not want to use rangechecking and then a try .. except (this would slow down code dramatically).

So I thought I might use Val('$0000000100000000', U, Err) (U being of type UInt64)

This has the advantage that any string which represents a value > High(QWord) wil set Err to > 0.
After that I would have to assign the value in U to W (after a check that the value in U is not "invalid" in the context of my code).

Bart

Reply by email

Jonas Maebe
to Bart
Bart wrote 14 lines
var W: Word; U: UInt64; {$R-} //range checkin off begin U := $0000000100000000; W := U; // or W := Word(U); end. I know that the value that i assigned to W is "wrong" (and I can handle that), but does it trash memory or cause other serious problems?

The only problem that can occur is a range check run time error in case range checking is enabled.

Jonas

Reply by email

Bart
to Jonas Maebe
Jonas Maebe wrote
The only problem that can occur is a range check run time error in case range checking is enabled.

Ok, thanks for the quick answer.
I actually need this in the fix for IntStrToTime() I'm working on.

Bart

Reply by email