Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-12

GetMem question

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

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. :)

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.

Graeme Geldenhuys Location: S 34° 03.168' E018° 49.342' http://tinyurl.com/y6lc26 There's no place like 127.0.0.1

Reply by email

Daniël Mantione
to Graeme Geldenhuys

Op Fri, 1 Dec 2006, schreef Graeme Geldenhuys:

Show quoted text 26 lines
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.

Show quoted text 10 lines
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.

Show quoted text 7 lines
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

Reply by email

Flávio Etrusco
to Graeme Geldenhuys: I got this question from a Delphi 2006 user regarding a code change I made in the tiOPF…

(....)

Show quoted text 11 lines
var lList : PPropList; 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 ---------------------------------------------

Simply put: the parameter is always discarded by GetMem, so the parameter should be declared 'out'.

Cheers,
Flávio

Reply by email