I have a unit which is for both Delphi & Lazarus. In Lazarus the unit compiled without any exception but in Delphi it gives me Error Data type too large: exceeds 2 GB. Below is the code:
unit UType;
{$ifdef FPC}
{$MODE delphi}{$H+}
{$endif}
interface
type
TFreqType = Extended;
TFreqCutArray = Array [0..0]of TFreqType;
PFreqCutArray = ^TFreqCutArray;
FilterOrder = Integer;
TAS_Sample = Extended;
TAS_SampleArray = Array[0..High(Integer) div Sizeof(TAS_Sample) - 1] of TAS_Sample;
PTAS_SampleArray = ^TAS_SampleArray;
TAS_Float = Extended;
TComplex = record
Re, Im: TAS_Sample; // Z = Re + i*Im
end;
PComplex = ^TComplex;
TComplexArray = Array[0..High(Integer) div Sizeof(TComplex) - 1] of TComplex;//here Delphi gives the error
PComplexArray = ^TComplexArray;
FilterProc = function(V: TAS_Sample): TAS_Sample of object;
implementation
end.
I am using Berlin Update 2, with the same code in Lazarus it compile without any error.
This seems like a compiler defect. You can declare the type like this:
TComplexArray = Array[0..67108862] of TComplex;
and the compiler will accept the declaration. Note that 67108862 = High(Integer) div Sizeof(TComplex) - 1
.
You can avoid hard coding the upper bound by declaring a constant:
const
ComplexArrayUpperBound = High(Integer) div Sizeof(TComplex) - 1;
type
TComplexArray = Array[0..ComplexArrayUpperBound] of TComplex;
This style of type declaration is very much out of fashion these days. I would strongly recommend that you use dynamic arrays. These will give you automatic clean up of dynamic memory, and allow the compiler to add range checking code for all your array access. That latter point is important since it will give you early warning of bounds errors in your code.
If you aren't allocating the arrays and instead declare these types to enable array indexing, then it is probably simpler to use {$POINTERMATH ON}
.
Furthermore I would suggest that you use Double
instead of Extended
. It is highly unlikely that you would have any need for the 10 byte Extended
type and switching to Double
will half your memory requirements. Because of alignment, your TComplex
is 32 bytes in size, but a Double
based version would be 16 bytes. This saving will result in significant performance benefit due to better use of the cache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With