[fpc-pascal] GetMem should use out parameter ?
Michael Van Canneyt
michael at freepascal.org
Wed Aug 22 12:05:43 CEST 2007
On Wed, 22 Aug 2007, Skybuck Flying wrote:
> Hello,
>
> Does GetMem care about the memory contents of the pointer variable ?
No.
>
> If not the parameter should be out instead of var to prevent the following
> hint message:
>
> GetMem( node, sizeof(Tnode) );
>
> Y:\Free Pascal\Units\Technical\TnodeList\version 0.08 port to free
> pascal\unit_TnodeList_version_009.pas(1015,3) Hint: Variable "node" does not
> seem to be initialized
It is so for compatibility reasons.
>
> GetMem could fail if out of memory, Delphi raises an OutOfMemory Exception.
>
> What does Free Pascal do ?
The same.
>
> Also I know when an exception is raised from inside a function the return
> value is never assigned to the variable.
>
> At least not the result variable/return value.
>
> However I am not so sure what happens to var parameters and out parameters,
> those can probably be changed, and later after they already changed an
> exception could be raised.
>
> So whatever bad happens GetMem could always make sure the parameter is
> initialized to nil ?!
There is no need; As soon as an exception is raised you know the value
in the pointer is invalid.
Michiael.
More information about the fpc-pascal
mailing list