Sign in Join

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

Mailing lists / fpc-pascal / 2006-08

Automatic objects

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

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)?

Marco van de Voort
to Lars Eriksen
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.

Micha Nelissen
to Marco van de Voort
Marco van de Voort wrote 5 lines
> 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

Lars Eriksen
to Marco van de Voort: The space of the object is cleaned. However if the object uses dyn memory, you will have…
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?

Marco van de Voort
to Lars Eriksen
Show quoted text 9 lines
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.