Sign in Join

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

Mailing lists / fpc-pascal / 2003-10

range-checking observations and questions

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Andy Sy

Apparently, activating -Cr is needed to enable enforcement of compile time constant range checking as well.

The ff. program:

program blah;
var
   E: array[2..4] of Integer;
begin
   E[10]:=15;
end.

will compile albeit with a warning. Only when -Cr is enabled will the range violation become an error. Is this kind of freedom intentional and will it serve certain purposes?

For arrays of characters, the range checking is even looser. I have noticed that the ff range violation:

program blah;
var
   D: array[2..4] of char;
begin
   D:='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   Writeln(D);
end.

is completely ignored whether -Cr is enabled or not. No error, no warning.

Question 1:
Will such code result in memory corruption somewhere in both the cases where runtime range checking is enabled and the one where it is not enabled?

Question 2:
Is this something that needs to be fixed or is there a philosophy/explanation behind this behaviour?

Question 3:
Is this a special case for char arrays/strings only and will never happen with other types?

Note: I am using FPC 1.0.10

Reply by email

Peter Vreman
to Andy Sy
Question 1: Will such code result in memory corruption somewhere in both the cases where runtime range checking is enabled and the one where it is not enabled?

No memory corruption. The length of the array is passed to the helper routine

Show quoted text 5 lines
Question 2: Is this something that needs to be fixed or is there a philosophy/explanation behind this behaviour?

A compile time warning can be added for the string to array assignment

Question 3: Is this a special case for char arrays/strings only and will never happen with other types?

char arrays are special, they are zero terminated strings and therefor allow assignment of constant string values. An array of byte can't be filled with a string.

Reply by email