Automatic objects

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Automatic objects

Lars Eriksen
Hello!

I've recently switched from Delphi to FP and never been exposed to
TP-style objects before.

At first sight they seem very similar to Delphi-style classes. However,
it seems it is possible to allocate them on the stack - very convenient.

Is it right to assume they are automatically deallocated when they go
out of scope (e.g. no need to free them manually)?


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Automatic objects

Marco van de Voort
> Is it right to assume they are automatically deallocated when they go
> out of scope (e.g. no need to free them manually)?

The space of the object is cleaned. However if the object uses dyn memory,
you will have to call the destructor (destroy) to make the object cleanup.

In general it is considered a good custom to destroy them.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Automatic objects

Lars Eriksen
Marco van de Voort wrote:

> The space of the object is cleaned. However if the object uses dyn memory,
> you will have to call the destructor (destroy) to make the object cleanup.
>
> In general it is considered a good custom to destroy them.

So no C++-style automatic deallocation (the destructor is called
automatically) is done?

If not, do neither classes nor objects provide such functionality?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Automatic objects

Micha Nelissen
In reply to this post by Marco van de Voort
Marco van de Voort wrote:
>> Is it right to assume they are automatically deallocated when they go
>> out of scope (e.g. no need to free them manually)?
>
> The space of the object is cleaned. However if the object uses dyn memory,
> you will have to call the destructor (destroy) to make the object cleanup.

I think in theory it's possible for the compiler to automatically call
the destructor always when leaving the function (think implicit
exception handlers here), but I guess not so many people are using them
anymore in new code that it would be worth it. Would need a new keyword
or something too, as it's not backward-compatible obviously.

Micha
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Automatic objects

Marco van de Voort
In reply to this post by Lars Eriksen
> Marco van de Voort wrote:
>
> > The space of the object is cleaned. However if the object uses dyn memory,
> > you will have to call the destructor (destroy) to make the object cleanup.
> >
> > In general it is considered a good custom to destroy them.
>
> So no C++-style automatic deallocation (the destructor is called
> automatically) is done?

No. Note that most local objects in C++ are strings and the like, but
strings and dynamic arrays are already first tier datatypes in FPC and
automatically managed.
 
> If not, do neither classes nor objects provide such functionality?

No. For classes it doesn't make sense anyway, since in the code a reference
could be passed to a different structure.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal