The old method of defining a static type of array with an open ended size, now fails a runtime.
sample:
PCells = array [0..3] of Byte;
TPArray = array [0..0] of PCells;
PTPArray = ^TPArray;
Now, as of FPC 3.2.0, I get range check fatal error on every attempt to access an array index > 0. Code that still runs OK in Delphi, and for the last 12 years in Lazarus, now fails.
The error generated is E1012: constant expression violates subrange bounds.Does it generate correct code in 64bit mode when the index is a 32bit integer _variable_ less than zero ?
Now, as of FPC 3.2.0, I get range check fatal error on every attempt to access an array index > 0. Code that still runs OK in Delphi, and for the last 12 years in Lazarus, now fails.
This code fails to compile in current Delphi versions as well (also with range checks disabled):
type PCells = array [0..3] of Byte; TPArray = array [0..0] of PCells; PTPArray = ^TPArray; var p: PTPArray; begin GetMem(p, 10 * SizeOf(PCells)); Writeln(p^[2]^); end.
The error generated is E1012: constant expression violates subrange bounds.
@Jonas: maybe this should be mentioned in User Changes 3.2.0 (https://wiki.freepascal.org/User_Changes_3.2.0#Dynamic_array_parameters_are_passed_like_pointers)?
But this code below works and it's the beauty of using static 0..0 arrays.
But this code below works and it's the beauty of using static 0..0 arrays.
array [0..0] means single element,
this is less than beautiful for larger arrays.
the easy fix which I am sure will work in all versions is this.
Type PCells = array [0..3] of Byte; PTPArray = ^PCells;
The error generated is E1012: constant expression violates subrange bounds.Does it generate correct code in 64bit mode when the index is a 32bit integer _variable_ less than zero ?
The issue is, the above works just fine in Delphi, and all previous FPC, except the latest 3.2.0, where it seems that runtime range checking of static arrays has been added. This is not mentioned in the notes (and not welcome either).
I wasn't asking about Delphi. It works in Delphi. I was asking if FPC 3.2.0 generates correct code when in 64bit mode with an index that is a 32bit integer variable with a value less than zero. (FPC 3.0.4 does not because it fails to sign extend the 32bit integer variable.)The error generated is E1012: constant expression violates subrange bounds.Does it generate correct code in 64bit mode when the index is a 32bit integer _variable_ less than zero ?
I don't have a 64-bit Delphi.
I wasn't asking about Delphi. It works in Delphi. I was asking if FPC 3.2.0 generates correct code when in 64bit mode with an index that is a 32bit integer variable with a value less than zero. (FPC 3.0.4 does not because it fails to sign extend the 32bit integer variable.)The error generated is E1012: constant expression violates subrange bounds.Does it generate correct code in 64bit mode when the index is a 32bit integer _variable_ less than zero ?
I don't have a 64-bit Delphi.
array [0..0] means single element, this is less than beautiful for larger arrays.
No. The beauty is that it's open ended for size, and the use of indexes at runtime, mean you are not constrained to declaring array sizes at design time. This was how you had to do it before dynamic arrays were available.
range checks are local, so this will also hide the errorThat is completely wrong.Now the arr variable will never be range checked.
program untitled; {$mode delphi}{$rangechecks on}{$pointermath on} var {$push}{$rangechecks off} arr:array[0..0] of byte; {$pop} // restore on. begin end.
I can't give you a simple example to test - simple examples don't crash. But a complex app with large mem usage is another story.
That is completely wrong.I saw it was wrong after testing. IMO it should work.
Range checking is a setting that applies to code, not to data.That is completely wrong.I saw it was wrong after testing. IMO it should work.
Without an example we can't help much.
Such code works for me with {$R-}.
And your code is different from your previous descriptions.
I thought $R- was the default? Seemed to be so before. You mean I have to force $R-, in every unit?