[fpc-devel] Wrong docs: not initialized global variables

Ondrej Pokorny lazarus at kluug.net
Tue Jul 3 19:27:18 CEST 2018


On 03.07.2018 19:00, Ralf Quint wrote:
> On 7/3/2018 9:40 AM, Ondrej Pokorny wrote:
>> 1.) SetLength itself produces an unitialized block of data within the
>> variable if the new length is bigger than the old length.
>>
>> Therefore it would be more appropriate to get an "uninitialized
>> warning" after SetLength:
>>
>> program Project1;
>> var
>>    S: string;
>> begin
>>    SetLength(S, 10); // <<< warning here     --->> WHY ????
>>    Writeln(S[2]);    // <<< no warning here  --->> WHY ????
>> end.
> The program does EXACTLY what you told it to do and the compiler gives
> you an explicit warning that that what you are doing is potentially
> wrong/dangerous.

No no no. You miss the point and you are wrong.

The compiler tells me that the line "SetLength(S, 10);" is potentially 
dangerous, which it is not.
The line "Writeln(S[2]);" is potentially dangerous, but the compiler 
does not warn me about it.
=> The compiler warns me about the wrong line.

See:

program Project1;
var
   S: string[10];
begin
   Writeln(S[2]);    // <<< warning here  --->> OK
end.

With your arguments I should get a warning while at line "S: 
string[10];" and no warning at "Writeln(S[2]);". Seriously?

> If you set the length of a string, without it having been
> initialized/assigned a value, all you do is to tell the program that you
> have now a string of a specified length. And as long as you do not
> exceed that specified length, there is no reason for the compiler to
> complain about it. YOU explicitly defined that accessing any
> character/element up to that defined length is valid!
> You as a programmer failed to heed the warning at the SetLength()
> function and adjusted your code accordingly.  You as a programmer should
> realize that there are random characters in the string if you extend the
> length of the string. How is the extended length to be initialized?
> Blanks (0x20), Zeroes (0x00)?

You obviously miss my point. Please read my previous email again. I 
don't feel the need to go into this discussion because it is off-topic.


> What about the following example?
>
> Program ABC;
> Var S : String;
> begin
>    S := '12345';
>    SetLength (S, 10);
>    WriteLn (S [2]);
>    WriteLn (S [7]);
> end.

I should get a warning in line "WriteLn (S [2]);". (Btw. if you read my 
previous email carefully you would see that this case is covered in 
point 5.)

The same as I get a warning in (the same example as above):

program Project1;
var
   S: string[10];
begin
   Writeln(S[2]);    // <<< warning here  --->> OK
end.

Ondrej



More information about the fpc-devel mailing list