[fpc-pascal] Questions regarding arrays

Honza befelemepeseveze at gmail.com
Wed Sep 22 11:42:35 CEST 2010


2010/9/22 Graeme Geldenhuys <graemeg.lists at gmail.com>:
> type
>  TAtomArray = array[0..0] of TAtom;
>  PAtomArray = ^TAtomArray;
>
> Now if I change TAtomArray to the follow, then my code doesn't work. :-)
>
>  TAtomArray = array of TAtom;   // a dynamic array
>
> So what exactly is the difference between these two?
>
>    TAtomArray = array[0..0] of TAtom;
>  vs
>    TAtomArray = array of TAtom;
>
>
> Is array[0..0] not actually a dynamic array, but just a static array
> with a single item? If so, then why does the returned value from the C
> API call, which returns a pointer to an array of culong's (normally
> more that one item) work? Somewhere I'm getting confused with all
> this, but would love to understand it correctly, and why it works. :)
>

In brief AFAIK:

type
  T1 = array[l..h] of T3;
  T2 = array of T3;

says that T1 is a value type and T2 is a pointer type (ptr casted
deref of a T2 typed variable may(? depends on implementation) safely
alias a T1 typed entity, but a pointer to a first element of a T1
value can't be safely aliased to a T2 typed entity). Additionally
dynamic arrays are a compiler handled structures, the differences are
similar to those between shortstrings and AnsiStrings types.

-- 
bflm
freepascal-bits.blogspot.com



More information about the fpc-pascal mailing list