I'm trying to get an 18-character space string to pre-pend to a header line> Looking up what's available, I found FillChar():
Program Example25;
{ Program to demonstrate the FillChar function. }
Var S : String[10];
I : Byte;
begin
For i:=10 downto 0 do
begin
{ Fill S with i spaces }
FillChar (S,SizeOf(S),' ');
{ Set Length }
SetLength(S,I);
Writeln (s,'*');
end;
end.
The example had me scratching my head, because it seems to avoid using FillChar to do what FillChar is advertised as doing, but, at any rate, I took the example as offered, and used it in my code:
FillChar (ListHeaderA, sizeof(ListHeaderA),' ');
SetLength(ListHeaderA,18);
FillChar (ListHeaderB, sizeof(ListHeaderB),' ');
SetLength(ListHeaderB,18);
My understanding is that the 18 bytes at ListHeaderA are filled with ' ', which means that the string (ListHeaderA) has an apparent length of 32 (the value of ' '). So I used SetLength, as in the example, to set the length to 18.
When I try to run the program, which compiles cleanly because, as the documentation says, no checking is done on the target of FillChar(), I get an address violation at $FFFFFFFFFFFFFFFF, at address 10008F1A.
Clearly something is wrong (and I don't like being able to wipe out the length of a string just like that).
Should I:
1) Use ListHeaderA[1] as the parameter to FillChar - gives Access VIolation one line earlier!
2) Copy the example and fill each byte individually (backwards (???)
3) Drop sizeof(ListHeaderA) etc and simply use 18 - gives Access Violation in the original line.
4) Try some other way of doing this (it's only done once, and I don't mind adding 18 spaces to an empty string. I just wanted to try FillChar.
Any enlightenment?
Please,
Tony