Sign in Join

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

Mailing lists / fpc-pascal / 2008-02

Turbo Pascal and Object Pascal ways of OOP

13 messages · 8 people ·

Reply by email Subscribe to this list Your subscription

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

Tiziano De Togni

From the Delphi (7) help and in the borland newgroups I read that the old TP style object type is considered to disappear, and actually only kept for code compatibility.

I see here that some developers still use the type object because of some advantages against the Class type (static allocation).

Can someone explain better these advantages?

My other question is:
Will Free Pascal still support the type object in the future or it is considered to disappear in the same way as it is expected in Delphi?

tia

tiziano ______________________________________ http://digilander.libero.it/tizzziano/
Michael Van Canneyt
to Tiziano De Togni
Tiziano De Togni wrote 9 lines
From the Delphi (7) help and in the borland newgroups I read that the old TP style object type is considered to disappear, and actually only kept for code compatibility. I see here that some developers still use the type object because of some advantages against the Class type (static allocation). Can someone explain better these advantages?

The advantage is mainly that you can have objects on the stack.

My other question is: Will Free Pascal still support the type object in the future or it is considered to disappear in the same way as it is expected in Delphi?

It will not disappear, but all new development is done using classes: the old objects will not evolve.

Michael.

Tiziano De Togni
to Michael Van Canneyt: The advantage is mainly that you can have objects on the stack. It will not disappear,…

Michael Van Canneyt ha scritto:

Show quoted text 13 lines
On Tue, 5 Feb 2008, Tiziano De Togni wrote: > From the Delphi (7) help and in the borland newgroups I read that the old TP > style object type is considered to disappear, and actually only kept for code > compatibility. > > I see here that some developers still use the type object because of some > advantages against the Class type (static allocation). > > Can someone explain better these advantages? The advantage is mainly that you can have objects on the stack.

sorry, but don't understand exactly what this means exactly.

I understand that if I write a procedure of this kind:

procedure processmyobject(myobject: tmyobject);
procedure processmyobject(myclass: tmyclass);

I have myobject on the stack, instead myclass is passed by reference, but don't see the advantage...

tiziano ______________________________________ http://digilander.libero.it/tizzziano/
Daniël Mantione
to Tiziano De Togni

Op Tue, 5 Feb 2008, schreef Tiziano De Togni:

Show quoted text 27 lines
Michael Van Canneyt ha scritto: > > On Tue, 5 Feb 2008, Tiziano De Togni wrote: > >> From the Delphi (7) help and in the borland newgroups I read that the old >> TP >> style object type is considered to disappear, and actually only kept for >> code >> compatibility. >> >> I see here that some developers still use the type object because of some >> advantages against the Class type (static allocation). >> >> Can someone explain better these advantages? > > The advantage is mainly that you can have objects on the stack. sorry, but don't understand exactly what this means exactly. I understand that if I write a procedure of this kind: procedure processmyobject(myobject: tmyobject); procedure processmyobject(myclass: tmyclass); I have myobject on the stack, instead myclass is passed by reference, but don't see the advantage...

Stack allocation is much faster than heap allocation, and automatic. Take a look at the matrix unit; you can return an object from a function without worrying about memory leaks, so they are very usefull in function results and operator overloading.

Further, objects allow you control the binary layout (the binary layout is just like a record), which means you can fit existing data structures (like those used in files) with methods. Some time ago we had a discussion about this list about mapping the GTK Gobject model to Pascal objects, succesfull experiments were done.

Daniël

Vinzent Hoefler
to Tiziano De Togni: Michael Van Canneyt ha scritto: sorry, but don't understand exactly what this means…
Tiziano De Togni wrote 5 lines
Michael Van Canneyt ha scritto: > > The advantage is mainly that you can have objects on the stack. sorry, but don't understand exactly what this means exactly.

The main difference is here:

-- 8< --
procedure Uses_Objects;
var
   My_Object : tMyObject;
   My_Class  : tMyClass;
begin
   My_Object.Init;
   My_Class := tMyClass.Create;
end {Uses_Objects};
-- 8< --

The memory allocation for the class can fail due to resource exhaustion and also is infinitely slower than the allocation for the object, which is there as soon as the procedure has been entered. On assembly level, it is only a different constant that the stack pointer is moved to make space for the local variables.

Referencing an instance being on the stack may also be a bit faster than referencing an instance being on the heap, because there's one level of dereference less, and it may be even more cache-friendly, because the stack is more likely to be in the cache already than a (newly) allocated memory block.

Meaning: Memory space and execution speed is more or less known beforehand and potential memory fragmentation problems are avoided.

Oh, and one thing more: No access violations when referencing an invalid (not yet initialized) instance. ;)

Vinzent.

Luiz Americo Pereira Camara
to Daniël Mantione: Op Tue, 5 Feb 2008, schreef Tiziano De Togni: Stack allocation is much faster than heap…
Daniël Mantione wrote

[..]

Show quoted text 10 lines
Stack allocation is much faster than heap allocation, and automatic. Take a look at the matrix unit; you can return an object from a function without worrying about memory leaks, so they are very usefull in function results and operator overloading. Further, objects allow you control the binary layout (the binary layout is just like a record), which means you can fit existing data structures (like those used in files) with methods. Some time ago we had a discussion about this list about mapping the GTK Gobject model to Pascal objects, succesfull experiments were done.

In a external c library, i have a structure like

TMyType = record
  x: Integer;
  y: Integer;
end;
PMyType = ^TMyType;

and a function that expects a pointer to that record

function DoIt(MyVar: PMyType);

can i safely use the below object instead of the record and pass directly to the c function?

TMyObj = object
  x: Integer;
  y: Integer;
  Method1;
  Method2;
end;
PMyObj = ^TMyObj;

Moreover, whats the difference between objects and records with methods (the new Delphi feature)?

Luiz

Daniël Mantione
to Luiz Americo Pereira Camara

Op Tue, 5 Feb 2008, schreef Luiz Americo Pereira Camara:

Show quoted text 10 lines
can i safely use the below object instead of the record and pass directly to the c function? TMyObj = object x: Integer; y: Integer; Method1; Method2; end; PMyObj = ^TMyObj;

Yes, objects (by specification) are defined to have the same binary layout as records.

Moreover, whats the difference between objects and records with methods (the new Delphi feature)?

Objects can have virtual methods, constructors, destructors. The new Delphi feature cannot.

Daniël

Marco van de Voort
to Daniël Mantione
Show quoted text 6 lines
Op Tue, 5 Feb 2008, schreef Luiz Americo Pereira Camara: > Moreover, whats the difference between objects and records with methods (the > new Delphi feature)? Objects can have virtual methods, constructors, destructors. The new Delphi feature cannot.

The records-with-methods feature originates from .NET, but some cases were made that they were also added to restrict the search for methods for duck-type style other new delphi features like for..in, and similar future expansion. Probably when these features are more closely regarded, additional reasons/behaviour will pop up.

A very rough summary of the msgs and (IRC-)discussions about the newer delphi features can be found here:

http://www.stack.nl/~marcov/delphilater.txt

Marc Weustink
to Daniël Mantione: Op Tue, 5 Feb 2008, schreef Luiz Americo Pereira Camara: Yes, objects (by specification)…
Daniël Mantione wrote 23 lines
Op Tue, 5 Feb 2008, schreef Luiz Americo Pereira Camara: > can i safely use the below object instead of the record and pass > directly to the c function? > > TMyObj = object > x: Integer; > y: Integer; > Method1; > Method2; > end; > PMyObj = ^TMyObj; Yes, objects (by specification) are defined to have the same binary layout as records. > Moreover, whats the difference between objects and records with > methods (the new Delphi feature)? Objects can have virtual methods, constructors, destructors. The new Delphi feature cannot.

however with virtuals or constructors/destructors, the memory layout is not 100% the same as a record.

Marc

Marc Weustink
to Vinzent Hoefler: The main difference is here: -- 8< -- procedure Uses_Objects; var My_Object : tMyObject;…
Vinzent Hoefler wrote 36 lines
On Tuesday 05 February 2008 12:02, Tiziano De Togni wrote: > Michael Van Canneyt ha scritto: >> The advantage is mainly that you can have objects on the stack. > sorry, but don't understand exactly what this means exactly. The main difference is here: -- 8< -- procedure Uses_Objects; var My_Object : tMyObject; My_Class : tMyClass; begin My_Object.Init; My_Class := tMyClass.Create; end {Uses_Objects}; -- 8< -- The memory allocation for the class can fail due to resource exhaustion and also is infinitely slower than the allocation for the object, which is there as soon as the procedure has been entered. On assembly level, it is only a different constant that the stack pointer is moved to make space for the local variables. Referencing an instance being on the stack may also be a bit faster than referencing an instance being on the heap, because there's one level of dereference less, and it may be even more cache-friendly, because the stack is more likely to be in the cache already than a (newly) allocated memory block. Meaning: Memory space and execution speed is more or less known beforehand and potential memory fragmentation problems are avoided. Oh, and one thing more: No access violations when referencing an invalid (not yet initialized) instance. ;)

Another difference, the memory of a class is zeroed on creation. This means that all membervars have the value of 0/nil, where for an object only automated member types are initialized.

Marc

Daniël Mantione
to Marc Weustink: however with virtuals or constructors/destructors, the memory layout is not 100% the same…

Op Tue, 5 Feb 2008, schreef Marc Weustink:

Show quoted text 27 lines
Daniël Mantione wrote: > > > Op Tue, 5 Feb 2008, schreef Luiz Americo Pereira Camara: > >> can i safely use the below object instead of the record and pass directly >> to the c function? >> >> TMyObj = object >> x: Integer; >> y: Integer; >> Method1; >> Method2; >> end; >> PMyObj = ^TMyObj; > > Yes, objects (by specification) are defined to have the same binary layout > as records. > >> Moreover, whats the difference between objects and records with methods >> (the new Delphi feature)? > > Objects can have virtual methods, constructors, destructors. The new Delphi > feature cannot. however with virtuals or constructors/destructors, the memory layout is not 100% the same as a record.

A constructor doesn't add a vmtlink. A virtual method does, and a destructor too because they are virtual. It is never a problem in practise, because you can declare the virtual method in a descendent, i.e.:

type  Tbinary_compatible_obj=object
         a,b,c,d,e,f:byte;
         constructor init;
       end;
       Tbinary_compatible_obj_with_vmt=object(Tbinary_compatible_record)
         constructur init;
         procedure virtualmethod;virtual;
         desctructor done;virtual;
       end;

In the above example, you can perfectly pass a
Tbinary_compatible_obj_with_vmt instance to any C library expecting a Tbinary_compatible_obj, because the vmtlink is located after the fields and therefore does not interfere with the binary structure.

Daniël

Marc Weustink
to Daniël Mantione
Daniël Mantione wrote 51 lines
Op Tue, 5 Feb 2008, schreef Marc Weustink: > Daniël Mantione wrote: >> >> >> Op Tue, 5 Feb 2008, schreef Luiz Americo Pereira Camara: >> >>> can i safely use the below object instead of the record and pass >>> directly to the c function? >>> >>> TMyObj = object >>> x: Integer; >>> y: Integer; >>> Method1; >>> Method2; >>> end; >>> PMyObj = ^TMyObj; >> >> Yes, objects (by specification) are defined to have the same binary >> layout as records. >> >>> Moreover, whats the difference between objects and records with >>> methods (the new Delphi feature)? >> >> Objects can have virtual methods, constructors, destructors. The new >> Delphi feature cannot. > > however with virtuals or constructors/destructors, the memory layout > is not 100% the same as a record. A constructor doesn't add a vmtlink. A virtual method does, and a destructor too because they are virtual. It is never a problem in practise, because you can declare the virtual method in a descendent, i.e.: type Tbinary_compatible_obj=object a,b,c,d,e,f:byte; constructor init; end; Tbinary_compatible_obj_with_vmt=object(Tbinary_compatible_record) constructur init; procedure virtualmethod;virtual; desctructor done;virtual; end; In the above example, you can perfectly pass a Tbinary_compatible_obj_with_vmt instance to any C library expecting a Tbinary_compatible_obj, because the vmtlink is located after the fields and therefore does not interfere with the binary structure.

it might be a compiler bug, but when I tried to zeromem a object having a constructor it resulted in an IE. Without a constructor not. That way I came to a conlusion that an object+constructor is not 100% the same as a simple record.

Marc