[fpc-pascal] Dynamic Array Length
Rainer Stratmann
RainerStratmann at t-online.de
Thu Dec 31 15:17:39 CET 2009
Hello Anthony,
all I want is that the compiler calculates the len of the const array.
const
data : array[ ... ] of byte = ( 11 , 12 , 13 );
datalen = sizeof( data ) div data[ 0 ];
In C it is possible.
I think in FPC not :-(
Best regards, Rainer
Am Thursday 31 December 2009 14:54:37 schrieb Anthony Walter:
> With Delphi/FPC you have a few choices:
>
> Fixed length arrays, dynamic length arrays, and variant type arrays
>
> Fixed length arrays cannot be resized and their lower and upper bounds are
> immutable. The lower and upper bounds can start at any ordinal position.
> The upper bound can ever be less than the upper bound. Also, you may use
> ordinal types as the bounds. Examples:
>
> var
> ZeroBased: array[0..9] of Integer;
> OneBased: array[1..10] of Integer;
> Balance: array[-1..1] of Integer;
> Data: array[Byte] of Byte;
> BoolStr: array[Boolean] of string;
>
> Dynamic length arrays are a managed type. That is to say the compiler
> generates to code to ensure the memory owned by the array is released when
> a procedure exits (if the array is a local). Kind of like the managed
> string type. You can use the SetLength() and Length() functions to
> resize/query them. You can also set them to nil, meaning an empty array.
> They cannot be used in a const block unless you set them to nil (which is
> kind of useless). Unlike the managed string type dynamic arrays do not copy
> on write. Dynamic arrays always use zero based indexing. Examples:
>
> var
> Names: array of string;
> JaggedInts: array of array of Integer;
>
> Both High and Low functions cna be used with fixed and dynamic arrays.
> Example:
>
> for X := Low(JaggedInts) to High(JaggedInts) do
> for Y := Low(JaggedInts[X]) to High(JaggedInts[X]) do
> DoSomething(JaggedInts[X, Y]);
>
> Variant type arrays are variants which reference TVarData having a VType of
> varArray. The following functions are used to manage variant arrays:
>
> VarArrayCreate
> VarArrayOf
> VarArrayDimCount
> VarArrayLowBound
> VarArrayHighBound
> ect
More information about the fpc-pascal
mailing list