Sign in Join

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

Mailing lists / fpc-devel / 2013-11

Hom to initialize an object (old style object, not TObject)

7 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Martin

If I have the code as shown at the end of the mail, i get

project1.lpr(6,9) Warning: Function result variable does not seem to
initialized

but how can I initialize it?

**** Unit1
unit Unit1;  {$mode objfpc}{$H+}
interface
uses  Classes, SysUtils;
type
   TBar = object
   private
     a: integer;
   public
     procedure Init;
   end;
implementation
procedure TBar.Init;
begin
   a := 0;
end;
end.

**** main program

program Project1;
uses Unit1;
function Foo: TBar;
begin
   Result.Init;
end;
begin
   Foo;
end.

Reply by email

Tomas Hajny
to Martin
Martin wrote 40 lines
If I have the code as shown at the end of the mail, i get project1.lpr(6,9) Warning: Function result variable does not seem to initialized but how can I initialize it? **** Unit1 unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils; type TBar = object private a: integer; public procedure Init; end; implementation procedure TBar.Init; begin a := 0; end; end. **** main program program Project1; uses Unit1; function Foo: TBar; begin Result.Init; end; begin Foo; end.

Sorry if I overlooked something, but where is the variable of that type declared in your code? These objects may be either declared statically ("var B: TBar") or dynamically ("Result := New (TBar.Init)" - but then the return value must be of type PBar, not TBar). If I understand it correctly, you initialize the object on the stack, but it would disappear from there (gets overwritten) immediately after the function return (the following function call would overwrite it).

Tomas

Reply by email

Martin
to Tomas Hajny
Tomas Hajny wrote
Sorry if I overlooked something, but where is the variable of that type declared in your code? These objects may be either declared statically ("var B: TBar") or dynamically ("Result := New (TBar.Init)" - but then the return value must be of type PBar, not TBar).

Objects are like (advanced) records.

So Result is a TBar, and result already has the memory it needs. "New" is not needed.

Show quoted text 5 lines
If I understand it correctly, you initialize the object on the stack, but it would disappear from there (gets overwritten) immediately after the function return (the following function call would overwrite it).
It gets lost only, because i do not use the result of the function call.
But even if D did "SomVarBar := Foo;", I still get the warning.

Reply by email

Tomas Hajny
to Martin: Objects are like (advanced) records. So Result is a TBar, and result already has the…
Martin wrote 10 lines
On 01/11/2013 15:10, Tomas Hajny wrote: > Sorry if I overlooked something, but where is the variable of that type > declared in your code? These objects may be either declared statically > ("var B: TBar") or dynamically ("Result := New (TBar.Init)" - but then > the > return value must be of type PBar, not TBar). Objects are like (advanced) records. So Result is a TBar, and result already has the memory it needs. "New" is not needed.

Result is stored in a temporarily allocated memory. You are right that New is not absolutely needed (as witnessed by the compiler accepting your code
- unlike TP/BP which wouldn't accept TBar as return value, because it doesn't support performing assignment on objects or even records), but using the temporarily allocated memory implies that you initialize the memory in one place first (Init) and then you need to copy the data to another memory location afterwards. Depending on the object size, this may or may not be an issue, but it doesn't look like the most efficient solution to me in any case.

Show quoted text 8 lines
> If I understand it > correctly, you initialize the object on the stack, but it would > disappear > from there (gets overwritten) immediately after the function return (the > following function call would overwrite it). It gets lost only, because i do not use the result of the function call. But even if D did "SomVarBar := Foo;", I still get the warning.

Yes. Still, if the (formally incorrect) warning helps in pointing out that the chosen solution may not be the most efficient one, it isn't so bad from my point of view. ;-)

Tomas

Reply by email