[fpc-pascal] Static Initialisation of Arrays
Jürgen Hestermann
juergen.hestermann at gmx.de
Tue Jun 29 19:14:02 CEST 2010
> spir schrieb:
> Sure, but this is just a matter of convention and support by a given compiler:
> type Integers = array of Integer;
> var ints : Integers = [1,2,3];
> could work as expected by automatically sizing, allocating and initialising.
> How else could work languages that have an array/sequence/list literal notation?
> Note that this is not much different from
> var text : String = "abc";
> since string vars must indeed be kinds of (pointed) dynamic arrays, I guess... (Or am I wrong on this?)
> The burden caused by the absence of such a notation for dynamic arrays is something we can accept and get used to; what advantage does it bring?
> It forces initialisation to be written in loops (if the content nicely follows a pattern) or sequences of element assignemnts (else).
> Maybe it's only me?
I don't know about the details of the compiler design but as far as I know, the data to initialize variables is stored in a fixed data record and its memory cannot be freed and reused by other variables (who request data on the heap). At least it was this way at turbo pascal times. Someone may correct me.
So if you initialze a string with "abc", then these 3 bytes (or 6 for widestrings) plus the ending zero byte is wasted once you assign another value to this string variable. It cannot be overwritten by other data. This does not matter much for (in general short) strings but for (in general huge) arrays this can be a problem.
On the other hand, if initialized (ansi)strings realy use data on the heap (which is freed when assigning other data) then I also don't unterstand why it's different with dynamic arrays.
More information about the fpc-pascal
mailing list