Re: pointer syntax



Maarten Wiltink wrote:
> "Bjørge" <bjorge@xxxxxxxxxxxxxxx> wrote in message
> news:4Z6dnWjEx7TyBB7e4p2dnA@xxxxxxxxxxxxxx
> [...]
>> procedure CheckCreateStringList(var List: TStringList);
>> begin
>> if not Assigned(List) then
>> List:=TStringList.Create;
>> end;
>>
>> procedure TForm1.Button1Click(Sender: TObject);
>> var
>> List: TStringList;
>> begin
>> CheckCreateList(List);
>> List.Free;
>> end;
>>
>> I guess someone would claim that this is strange coding style,
>> but it's perfectly valid ;-)
>
> Er, no. List contains garbage at the moment you pass it to
> CheckCreateStringList which inspects and possibly assigns it.
> Things will go horribly wrong if the space it takes up was
> previously in use by an Integer with the value three.

Oh yeah. Forgot the initialization

List:=nil;

....and I think for the nth time: "Why arent those initialized to nil in the
first place ?"

> Making it an out parameter would make the call valid again, but
> invalidate the test inside the procedure. It's a logical error
> to read an out parameter until you've assigned it a value.

Of course. What puzzles me is this: With strings and dynamic arrays it's
taken care of (the nil initialization, that is). Why not with other pointer
types ?
--
Bjørge
bjorge@xxxxxxxxxxxx


.


Quantcast