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.
Maybe the StringOfChar(' ', length) should be called as part of the default initialization.
I don't think so. This can be quite some effort, which is not normally wanted. If you assign a string of length 3 to this variable, it needs to set the length and 3 char fields. Blanking the remaining 497 fields is just unnecessary work, unless the programmer has very special needs, but that's not the default.
Frank