Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-06

Static Initialisation of Arrays

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Paul Michell

Hello All,

I am working on a program that requires some simple embedded array data. I can get the following to compile:

Type
   TCharacterAreaData = Record
     Code: Integer;
     Name: String;
     Geometry: Array [0..3] Of Single;
   End;
Const
   Area1: TCharacterAreaData = (Code: 1;
                                Name: 'Border Moors and Forests';
                                Geometry: (1,2,3,4));

My problem is the number of elements in the Geometry Array is variable. So if I try this:

Type
   TCharacterAreaData = Record
     Code: Integer;
     Name: String;
     Geometry: Array Of Single;
   End;
Const
   Area1: TCharacterAreaData = (Code: 1;
                                Name: 'Border Moors and Forests';
                                Geometry: (1,2,3,4));
   Area2: TCharacterAreaData = (Code: 2;
                                Name: 'Bowland Fells';
                                Geometry: (1,2,3,4,5,6));

I get this syntax error:

Areas.pas(46,42) Fatal: Syntax error, "NIL" expected but "(" found

I realise that 'Array Of Single' declares a dynamic array, but is there any equivalent syntax for static data arrays in the same way that strings litterals are in effect, variable length static declarations.

Thanks in advance,

Paul Michell

P.S. I'm using FPC 2.4.1 i386-win32-win32/win64

Jonas Maebe
to Paul Michell
Paul Michell wrote
I realise that 'Array Of Single' declares a dynamic array, but is there any equivalent syntax for static data arrays in the same way that strings litterals are in effect, variable length static declarations.

No, there is not...

Jonas

Paul Michell
to Jonas Maebe

Thanks, that will save me some futile googling ;)

Jonas Maebe wrote 23 lines
On 28 Jun 2010, at 11:17, Paul Michell wrote: > I realise that 'Array Of Single' declares a dynamic array, but is > there any equivalent syntax for static data arrays in the same way > that strings litterals are in effect, variable length static > declarations. No, there is not... Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.830 / Virus Database: 271.1.1/2967 - Release Date: 06/27/10 19:35:00
Jürgen Hestermann
to Paul Michell: I am working on a program that requires some simple embedded array data. I can get the…
I realise that 'Array Of Single' declares a dynamic array, but is there any equivalent syntax for static data arrays in the same way that strings litterals are in effect, variable length static declarations.

You have to realize, that dynamic arrays are actually pointers (to the array and other administration data). So you cannot define anything to such arrays at declaration time because no memory is assigned to this pointer yet. You have to use setlength to assign memory at runtime (just as getmem would do) and only then you can fill it with data.

spir
to Jürgen Hestermann
Show quoted text 8 lines
Paul Michell schrieb: > I realise that 'Array Of Single' declares a dynamic array, but is there > any equivalent syntax for static data arrays in the same way that > strings litterals are in effect, variable length static declarations. You have to realize, that dynamic arrays are actually pointers (to the array and other administration data). So you cannot define anything to such arrays at declaration time because no memory is assigned to this pointer yet. You have to use setlength to assign memory at runtime (just as getmem would do) and only then you can fill it with data.
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?

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Jürgen Hestermann
to spir
Show quoted text 12 lines
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.

Vinzent Höfler
to spir: On Mon, 28 Jun 2010 18:17:47 +0200 Sure, but this is just a matter of convention and…

spir :

Show quoted text 7 lines
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?

How else!? Are you seriously asking that?

By statically determining the array bounds from the initialization expression, of course? C does it that way, Ada does, too, other languages also. In both implementations it is still not a dynamic array, because although the space may be allocated "dynamically" (on the stack), the bounds do not change anymore, so the size remains static after the declaration.

Apart from some obscure algorithms ;) that rely on reallocation and dynamically changing the size, such a one-time allocation is even the most common use of "dynamic" arrays.

Vinzent.

GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01