comp.lang.ada
 help / color / mirror / Atom feed
* Finalization and Declare blocks.
@ 2002-12-21  8:51 Stapler
  2002-12-21 15:13 ` James S. Rogers
  0 siblings, 1 reply; 2+ messages in thread
From: Stapler @ 2002-12-21  8:51 UTC (permalink / raw)


I'm currently a little fuzzy on how Ada compilers handle allocation and
de-allocation.

If I declare an Abstract Data Type(a limited record for example) in the
private part of a package, with a generic Item as one of it's elements,
and then I "with" that package in another package, can I instantiate the
data type without using "new" and also assume that the the Data Type will
be automatically de-allocated at the end of the declare block?

Or, within the declaration part of the declare block, could I derive the
Abstract Data Type from Ada.Finalization.Controlled and assume the
same?(That it will be automatically de-allocated at the end of the
declare block.)

I've been getting mixed messages, so I'm assuming it depends on who's
compiler I'm using.

Any pointers would be appreciated.

Stapler



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Finalization and Declare blocks.
  2002-12-21  8:51 Finalization and Declare blocks Stapler
@ 2002-12-21 15:13 ` James S. Rogers
  0 siblings, 0 replies; 2+ messages in thread
From: James S. Rogers @ 2002-12-21 15:13 UTC (permalink / raw)


"Stapler" <spam.magnet@yahoo.com> wrote in message
news:IYVM9.245693$pN3.19944@sccrnsc03...
> I'm currently a little fuzzy on how Ada compilers handle allocation and
> de-allocation.
>
> If I declare an Abstract Data Type(a limited record for example) in the
> private part of a package, with a generic Item as one of it's elements,
> and then I "with" that package in another package, can I instantiate the
> data type without using "new" and also assume that the the Data Type will
> be automatically de-allocated at the end of the declare block?
If you create the instance within a declare block, without using "new",
the object will be deallocated upon exiting the block. The object
resides on the stack. When the block goes out of scope the stack
memory associated with that block is reclaimed.

>
> Or, within the declaration part of the declare block, could I derive the
> Abstract Data Type from Ada.Finalization.Controlled and assume the
> same?(That it will be automatically de-allocated at the end of the
> declare block.)
If you derive the data type from Ada.Finalization.Controlled, or
Ada.Finalization.Limited_Controlled, the finalization procedure will
be called when the object goes out of scope. You should override the
finalization procedure to handle deallocation.

Controlled and Limited_Controlled types do not provide automatic
garbage collection. You need to explicitly design their Finalize
procedures to deallocate memory. The advantage of Controlled and
Limited_Controlled types is that you can override the Finalize
procedure to do what you want. Finalize will be called whenever
the Controlled object goes out of scope, or whenever a new
instance of a Controlled object is assigned to an access value
for that type.
>
> I've been getting mixed messages, so I'm assuming it depends on who's
> compiler I'm using.

No, this is not a compiler issue. All Ada compilers should give you the
same behavior.

Jim Rogers





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-12-21 15:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-21  8:51 Finalization and Declare blocks Stapler
2002-12-21 15:13 ` James S. Rogers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox