On Sun, 2 Apr 2006, Waldek Hebisch wrote:
Russell Whitaker wrote:
hi
The following compiles but no longer works:
program fill; var s: string(10); begin fillchar(s[1], s.capacity, ' '); writeln("ok"); end.
Do you get a message about out of range value? If yes then strictly speaking the compiler is right: s is uninitialized, so its length is set to 0, hence access to s[1] is illegal (out of range). In fact, I see another problem with the code above: s[1] is a single char, but fillchar acceses also s[2],... which strictly speaking is another out of bound access (but ATM compiler can not detect this problem).
I would say that fillchar is incompatible with range checking (at least now). ATM you can say:
setlength(s, 10); fillchar(s[1], s.capacity, ' ');
Thanks. Adding setlength did the job.
and I will work. However, I think that the correct way is:
setlength(s, s.capacity); fillchar(s[1..length(s)], length(s), ' ');
shouldn't that be fillchar(s[1], length(s), ' '); ?
In the future we may teach the compiler that fillchar is special, so it applies to it relaxed range checking rules, but that would require non-trivial changes in the compiler.
perhaps something like: if ( start+count ) GT s.capacity then out-of-range-error if ( start+count ) GT length(s) then setlength( s, start+count)
Also, if the fillchar line is written as fillchar( s, count, Afillchar ); it should default to fillchar( s[1], count, Afillchar );
current behavior fillchar( s, ... overwrites length (and capacity?) fields.
Russ