On Sun, 7 Aug 2005, Frank Heckenbach wrote:
CBFalconer wrote:
Frank Heckenbach wrote:
Russell Whitaker wrote:
This used to work:
... before GPC did range checking. ;-)
program fil; var i : integer; s : string( 500 ); begin for i := 1 to 500 do s[ i ] := ' '; writeln("ok"); end.
Now I get "Value out of range"
The length of the string is uninitalized (and probably happens to be 0), so accesss to characters > 0 (i.e., all ;-) is out-of range.
You could do `SetLength (s, 500)' before the loop, or `uses GPC; [...] s := StringOfChar (' ', 500);' instead of the loop.
This certainly sounds like a semantics problem to me. I would normally expect to see the string extended by the writing action, with the range error based on the capacity. I see no easy answer that will be both secure and expected. Obviously there is automatic string initialization going on, and it sounds as if it is the simplest, i.e. the capacity and length.
No, it isn't. As I wrote, the length is uninitialized (i.e., undefined). It usually *happens* to be 0 for a global variable, and GPC doesn't check for undefined values.
The capacity, OTOH, is initialized, and must be in order for the string to work at all.
Would it be reasonable to define the length to be 0 as the default initialization? Otherwise the following should produce an error but dosen't:
program foo; var s : string( 500 ); begin writeln( length(s) ); end.
As for the original problem, many thanks to Frank, Chuck F, HF, and Gale Paeper for their help.
Russ