Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FillChar

145 views
Skip to first unread message

Steve

unread,
Dec 12, 2003, 11:48:32 AM12/12/03
to
Hello,

I have a 2D array of Word, what is the fastest way to fill with the word max
value (65535) using fillchar?

thanks

Steve


John Herbster (TeamB)

unread,
Dec 12, 2003, 11:54:00 AM12/12/03
to

"Steve" wrote
> I have a 2D array of Word, what is the fastest way to fill ...

The best answer will depend on whether it is a
dynamic array or a fixed size array? Rgds, JohnH

Christen Fihl

unread,
Dec 12, 2003, 4:56:27 PM12/12/03
to
FillChar(MyArray, SizeOf(Myarray), -1);

Note the syntax, as it is shown wrong in other answer.

This works as 65535 is hex $FFFF, the same as 2 bytes of $FF


Christen Fihl
http://HSPascal.Fihl.net/


Dave Hackett

unread,
Dec 12, 2003, 4:47:21 PM12/12/03
to
var
MyArray := array[1..65535, 1..65535] of word;
...
fillchar(MyArray, 0, SizeOf(MyArray));
//change 0 to whatever you want to fill the array with

- or -

type
PMyArray := ^TMyArray;
TMyArray := array[1..65535, 1..65535] of word;
var
MyArray := PMyArray;
...
new(MyArray);
fillchar(MyArray^, 0, SizeOf(TMyArray));


DaveH

"Steve" <nos...@nospam.com> wrote in message
news:3fd9...@newsgroups.borland.com...

Dennis

unread,
Dec 13, 2003, 1:26:48 PM12/13/03
to
Hi

Remember to use a Fastcode FillChar.

http://home3.gvdnet.dk/~marianndkc/FillcharChallenge.htm

Regards
Dennis


Steve

unread,
Dec 15, 2003, 2:54:32 AM12/15/03
to

Sorry for my late reply.

I use a 2D dynamic array.

Steve

unread,
Dec 15, 2003, 2:57:06 AM12/15/03
to
Christen,

Does this also apply to Dynamic array?

thanks

Steve

Steve

unread,
Dec 15, 2003, 3:14:36 AM12/15/03
to

Dave,

with a dynamic array I have done this but it crashes.

FillChar(Matrix, Width * Height * 2, -1);

Christen Fihl

unread,
Dec 15, 2003, 3:24:18 AM12/15/03
to
Sure


Steve

unread,
Dec 15, 2003, 3:40:51 AM12/15/03
to
It fails...

I have TMatrix = array of array of word;

SetLength(Matrix,Width , Height );

If I do FillChar(Matrix, Width * Height * 2, -1); it freezes the app

Steve


Christen Fihl

unread,
Dec 15, 2003, 4:58:52 AM12/15/03
to
Ups, I need a bit more explanation.
When using dynamic String and dynamic arrays with FillChar, you need to use
the address of the data structure, not just the variable name, in your case
Matrix.
Also you need to make sure there actually is data in the structure. Else you
will be working on NIL, same as address 00000000.

In dynamic arrays, 2 dimensionals are saved with one dimension as an array
of "pointers" to the other dimension.
In plan arrays, the data is contigous, not here. You can only FillChar one
dimension at a time.

In Strings you could do: FillChar(S[1], Length(S), 'F');
But not: FillChar(S, Length(S), 'F'); !!!

This example works.

Type Elem= Word;
TMatrix = array of array of Elem;
var Matrix: TMatrix;
N: Integer;
Const Width=2; Height=3;
begin
SetLength(Matrix, Width, Height);
for N:=Low(Matrix) to High(Matrix) do
FillChar(Matrix[N,0], Height * SizeOf(Elem), -1);
end.


Remember to use: Matrix[N,0] in FillChar.
Your example would fill the 4 bytes (the Matrix pointer) and much more
around it, kill the stack contenst.

--
Christen Fihl
http://HSPascal.fihl.net


Jens Gruschel

unread,
Dec 15, 2003, 5:27:15 AM12/15/03
to
> In dynamic arrays, 2 dimensionals are saved with one dimension as an array
> of "pointers" to the other dimension.

That's why sometimes it might be an idea to use a one-dimensional array, set
its length to width * height, and address its single elements with index :=
width * y + x (where x in [0..width-1] and y in [0..height-1]). It makes the
use of FillChar much easier. And although it looks like a one-dimensional
array, it comes closer to a static two-dimensional array, than a dynamic
array of array (from the "behind the scenes" point of view). But it all
depends. Using an array of array tends to be more readable and is a must if
you want to change the length of a single row.

Jens

Steve

unread,
Dec 15, 2003, 7:12:03 AM12/15/03
to
Thanks Christen, it works now.

Steve


Peter Below (TeamB)

unread,
Dec 15, 2003, 3:03:07 PM12/15/03
to
In article <3fdd...@newsgroups.borland.com>, Steve wrote:
> Sorry for my late reply.
>
> I use a 2D dynamic array.

That is hard to fill efficiently since the array memory is not in one
block. A 2D dynamic array is in fact an array of pointers, each of
which points at a 1D dynamic array. If the rightmost dimension of the
array is small you will not get much improvement over the standard loop
over all elements. If it is larger you may benefit from something like
this:

type
T2DWordarray = array of array of Word;

procedure Init2DArrayOfWord( A: T2DWordArray );
var
i: Integer;
begin
for i:= 0 to High(A) do
FillChar( A[i,0], Length( A[i] )*Sizeof(A[i,0]), $FF );
end;


--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


0 new messages