[fpc-pascal] Turbo Pascal and Object Pascal ways of OOP

Vinzent Hoefler JeLlyFish.software at gmx.net
Tue Feb 5 12:17:01 CET 2008


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


Vinzent.



More information about the fpc-pascal mailing list