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

Marc Weustink marc at dommelstein.net
Tue Feb 5 23:25:57 CET 2008

Vinzent Hoefler wrote:
> 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.


More information about the fpc-pascal mailing list