Sign in Join

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

Mailing lists / fpc-pascal / 2007-08

GetMem should use out parameter ?

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Skybuck Flying

Hello,

Does GetMem care about the memory contents of the pointer variable ?

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

GetMem could fail if out of memory, Delphi raises an OutOfMemory Exception.

What does Free Pascal do ?

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 ?!

What your thoughts on this ?

Bye,
Skybuck.

Reply by email

Skybuck Flying
to Skybuck Flying

Never mind.

I see what the problem is.

GetMem is actually a kind of (probably magical) wrapper around a memory manager.

The real GetMem is a function which returns a pointer.

As I already wrote, in Delphi an out of memory exception is raised.

The result value is thus never returned, which in this case means GetMem can not return a nil pointer.

And the result will be an uninitialized variable.

This creates a tiny little problem to get rid of the hint message.

It can probably easily be solved by writing:

Node := nil;
GetMem( Node, SizeOf(TNode) );

Bye,
Skybuck.

Reply by email

Michael Van Canneyt
to Skybuck Flying: Does GetMem care about the memory contents of the pointer variable ? If not the parameter…
Skybuck Flying wrote
Hello, Does GetMem care about the memory contents of the pointer variable ?

No.

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

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

Reply by email

mm
to Skybuck Flying: Does GetMem care about the memory contents of the pointer variable ? If not the parameter…
Skybuck Flying wrote 16 lines
Hello, Does GetMem care about the memory contents of the pointer variable ? 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 GetMem could fail if out of memory, Delphi raises an OutOfMemory Exception. What does Free Pascal do ?

I believe it depends on the global variable "ReturnNilIfGrowHeapFails". When it is false, FPC behaves like Delphi.

mm

Reply by email