Discussion:
[fpc-pascal] Array initialization
Francisco Reyes
2009-03-17 23:50:21 UTC
Permalink
On the following URL,
http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212,
I found some different syntax to initialize an array.

Examples there..
var
c1 : array[1..10] of char := '123456';
int2 : array[1..100] of integer := [50 of 1, 50 of 2];

Tried those, but didn't seem to to work.

Is a loop the only way to initialize all the values in an array other than
Values: array[1..2] of integer = (0,0);

In the program I am working on, Values will be an array of 128 integers and
I would like to initialize them to 0.

Right now I just using:
for InitialLoop := 1 to 128 do
Values[InitialLoop] :=0;
Fantomas
2009-03-18 00:25:26 UTC
Permalink
Hello, Francisco!
Post by Francisco Reyes
Is a loop the only way to initialize all the values in an array other than
Values: array[1..2] of integer = (0,0);
In the program I am working on, Values will be an array of 128 integers and
I would like to initialize them to 0.
for InitialLoop := 1 to 128 do
Values[InitialLoop] :=0;
As far as I know, global variables are initialised with zeros when an
application starts. But to ensure your array is filled with zeros you can use
FillChar:

FillChar(Values,SizeOf(Values),0)

--
Best regards,
Fantomas
Francisco Reyes
2009-03-18 12:18:33 UTC
Permalink
Post by Fantomas
As far as I know, global variables are initialised with zeros when an
application starts.
Gives a warning when I don't initialize it.
split.pas(31,55) Warning: Variable "Values" does not seem to be initialized
Post by Fantomas
FillChar(Values,SizeOf(Values),0)
That worked.
However, for an integer is fillword better?
Jürgen Hestermann
2009-03-18 17:33:33 UTC
Permalink
Post by Francisco Reyes
Post by Fantomas
FillChar(Values,SizeOf(Values),0)
That worked.
However, for an integer is fillword better?
A "fillword" procedure does not exist. FillChar is an anchient Pascal
function to simply fill any arbitrary memory location of any size
(determined by a variable) with all the same byte. It is very fast but
there is no type checking at all (you can fill any type of variable) and
I think there is no range checking either. So you should use SizeOf() to
give the number of bytes to be written but in special circumstance you
can also use a different value.

A similar procedure is move, which copies bytes from one memory location
to another (again both determined by variables) also without type and
range checking.

Ju"rgen Hestermann.
Jürgen Hestermann
2009-03-18 17:41:28 UTC
Permalink
Post by Jürgen Hestermann
A "fillword" procedure does not exist.
It seems I was wrong about this. A "fillword" procedure DOES exist. I
just discovered this myself. But then the range (size of variable) you
want to fill has to be an even number of bytes. In general this may not
be guaranteed.

Jürgen Hestermann.

Andrew Brunner
2009-03-18 01:57:37 UTC
Permalink
var
c1 : array[1..10] of char;
begin
fillchar(c1[1],5,0);
fillchar(c1[5],5,#32);

You might need @ sign in front of c1 though...
...or...

const
c1:array[1..10] of char = '123456789a';
Post by Francisco Reyes
On the following URL,
http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212,
I found some different syntax to initialize an array.
Examples there..
var
c1 :  array[1..10] of char := '123456';
int2 : array[1..100] of integer := [50 of 1, 50 of 2];
Tried those, but didn't seem to to work.
Is a loop the only way to initialize all the values in an array other than
Values: array[1..2] of integer = (0,0);
In the program I am working on, Values will be an array of 128 integers and
I would like to initialize them to 0.
for InitialLoop := 1 to 128 do
       Values[InitialLoop] :=0;
_______________________________________________
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
leledumbo
2009-03-18 03:33:52 UTC
Permalink
Post by Francisco Reyes
On the following URL,
http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212,
I found some different syntax to initialize an array.
That's another Pascal extension from Sun. One of the problem (and power) of
Pascal is that it's often extended arbitrarily, because no standard (other
than too simple ISO 7185 and too complicated ISO 10260) exists.

Remember that FPC tries to be compatible with Delphi (at least until version
7) and Turbo Pascal (also version 7), while adding its own extensions.
Post by Francisco Reyes
c1 : array[1..10] of char := '123456';
In this case, FPC still follows (not 100 % conforms) the standard. c1 is a
fixed length array, therefore only strings of the same length can be
assigned to it. If you want to follow standards, pad it with spaces.
Otherwise, use String[10].
Post by Francisco Reyes
int2 : array[1..100] of integer := [50 of 1, 50 of 2];
This is like C's array initialization (extended maybe), which is not
supported. Either fill all or do it in a statement. You can fill some
followed by FillXXX which is the most commonly used method as it doesn't
involve looping (MUCH faster).

IMHO, FPC should consider this kind of extension though (partial filling).
Post by Francisco Reyes
begin
fillchar(c1[1],5,0);
fillchar(c1[5],5,#32);
AFAIK, no. FillChar doesn't expect pointers, the above is already correct
(note that it's syntactically correct, but it will fill the wrong data if
you add @).
--
View this message in context: http://www.nabble.com/Array-initialization-tp22570315p22572320.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
Marc Santhoff
2009-03-18 06:35:32 UTC
Permalink
Post by Francisco Reyes
On the following URL,
http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212,
I found some different syntax to initialize an array.
Examples there..
var
c1 : array[1..10] of char := '123456';
int2 : array[1..100] of integer := [50 of 1, 50 of 2];
Tried those, but didn't seem to to work.
Use only '=' instread of ':='.

Working is e.g.:

<code>
vector: array[0..6] of longint = (1, 2, 3, 4, 5, 6, 7);

type
particle_t = record
name: array [0..Pred(16)] of char;
longi: integer;
pressure: float;
temperature: double;
lati: integer;
end;

wbuf: array [0..Pred(NRECORDS_C)] of particle_t = (
(name:'zero'#0; longi:0; pressure:0.0; temperature:0.0; lati:0),
(name:'one'#0; longi:10; pressure:1.0; temperature:10.0; lati:10),
...
</code>
Post by Francisco Reyes
Is a loop the only way to initialize all the values in an array other than
Values: array[1..2] of integer = (0,0);
In the program I am working on, Values will be an array of 128
integers and
I would like to initialize them to 0.
Use fillchar() as others mentioned.
--
Marc Santhoff <***@web.de>
Marco van de Voort
2009-03-18 17:38:22 UTC
Permalink
Post by Jürgen Hestermann
Post by Francisco Reyes
Post by Fantomas
FillChar(Values,SizeOf(Values),0)
That worked.
However, for an integer is fillword better?
A "fillword" procedure does not exist.
http://www.freepascal.org/docs-html/rtl/system/fillbyte.html
http://www.freepascal.org/docs-html/rtl/system/fillword.html
http://www.freepascal.org/docs-html/rtl/system/filldword.html

(byte is the same as char)
Post by Jürgen Hestermann
A similar procedure is move, which copies bytes from one memory location
to another (again both determined by variables) also without type and
range checking.
Compare* and index* functions also belong in this category. In general you
could say they are memory-block operations.
Continue reading on narkive:
Loading...