[fpc-pascal] GetMem question
Daniël Mantione
daniel.mantione at freepascal.org
Fri Dec 1 09:07:13 CET 2006
Op Fri, 1 Dec 2006, schreef Graeme Geldenhuys:
> I got this question from a Delphi 2006 user regarding a code change I
> made in the tiOPF project. tiOPF is compiled with FPC using the
> Delphi compiler mode. FPC 2.0.4 and FPC 2.1.1 is used.
>
> ---------------------------------------------
> procedure tiGetPropertyNames(AClass : TtiBaseObjectClass;
> AStringList : TStringList; APropFilter : TTypeKinds = ctkSimple);
> var
> lCount : integer;
> lSize : integer;
> lList : PPropList;
> i : integer;
> lPropFilter : TTypeKinds;
> begin
> lList := nil; // <<== New line, I expect to fix a FPC warning
> ...
> GetMem(lList, lSize);
>
> But it's created a warning in BDS2006
>
> Variable assigned to LList never used
> ---------------------------------------------
>
> Quote from the FPC's GetMem help:
> Getmem reserves Size bytes memory on the heap, and returns a pointer
> to this memory in p. If no more memory is available, nil is returned.
This is incorrect. This behaviour depends on ReturnNilIfGrowHeapFails. If
set to true, nil is returned, otherwise, a runtime error is generated.
> Quote from Kylix 3 help:
> If there isn't enough memory available to allocate the dynamic
> variable, an EOutOfMemory exception is raised.
>
>
> Question 1:
> I set lList := nil; to get rid of a FPC compiler warning saying I am
> using a variable that hasn't been initialized. Is that warning needed
> in such a case, because GetMem will initialize it for me, correct?
> Ignoring the compiler warning is also a option I guess. :)
Getmem should initialize the variable.
> Question 2:
> Reading the help, I thought there would be no harm setting lList :=
> nil, because if there is no memory available, GetMem would set it to
> nil as well. But this brings me to the second question. Under Delphi
> and Kylix, an EOutOfMemory exception is raised, but under FPC the
> pointer is set to nil. Does FPC also raise an EOutOfMemory exception?
> If it does, the help doesn't mention it.
If sysutils is used, FPC converts runtime errors into exceptions, and
EOutOfMemory is generated.
Daniël
More information about the fpc-pascal
mailing list