comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <no.spam@no.spam.com>
Subject: Memory management clarification
Date: Tue, 26 Jul 2005 11:57:17 +0200
Date: 2005-07-26T11:57:17+02:00	[thread overview]
Message-ID: <dc51dt$qi6$1@sunnews.cern.ch> (raw)

Hi,

Trying to learn a bit of Ada I came across a statement that memory 
allocated from the pool will be implicitly reclaimed when the acces 
variable used to reference it goes out of scope.
That's nice, but I would like to learn a bit more about the exact 
mechanics of this and about the guarantees that it can provide.

Let's say that there is some MyType definition and this:

type MyTypeRef is access MyType;

1.

declare
     X : MyTypeRef;
begin
    loop
       X := new MyType;
    end loop;  -- infinite loop just for the sake of discussion
end;

Note that X does not goes out of scope when the loop is executing.
Will the memory be reclaimed? When? What can be said about the memory 
consumption of such program? Is it bounded and guaranteed? Is it 
necessarily larger than without the loop (just single allocation)?

2.

loop
    declare
       X : MyTypeRef;
    begin
       X := new MyType;
    end;
end loop;

What now? Is this any different from the memory management point of view?

3.

declare
    X : MyTypeRef;
begin
    X := new MyType;
    X := new MyType;
    X := new MyType;
    X := new MyType;
    -- ...
end;

When is the memory reclaimed for each allocated object? At each 
subsequent assignment? Or maybe at the end of the block? Or even 
"sometime later"? Or maybe all subsequent assignments are eliminated by 
compiler?

4.

Is it possible to associate some function with object allocated by new, 
which would be called at the time (or maybe after) the object is reclaimed?
Yes, I'm asking about destructors or finalizers.

5.

Is it possible to "overload" new for MyType so that the X := new MyType; 
statement will do whatever *I* want it to do, including actual memory 
allocation? If yes, is it possible to hook on memory reclamation as well?

6.

What about reference cycles between dynamically allocated objects?

declare
    type ListNode;
    type ListNodeRef is access ListNode;
    type ListNode is record
       SomeData : Integer;
       Other : ListNodeRef;
    end record;
    First, Second : ListNodeRef;
begin
    First := new ListNode;
    First.all.SomeData := 7;
    Second := new ListNode;
    Second.all.SomeData := 8;

    First.all.Other := Second;
    Second.all.Other := First; -- cycle
end;

Will the memory be reclaimed?


Regards,


-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



             reply	other threads:[~2005-07-26  9:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-26  9:57 Maciej Sobczak [this message]
2005-07-26 10:38 ` Memory management clarification Adrien Plisson
2005-07-26 14:19   ` Robert A Duff
2005-07-26 13:57 ` Frank J. Lhota
2005-07-26 14:21   ` Robert A Duff
2005-07-26 18:11     ` Frank J. Lhota
2005-07-26 14:17 ` Robert A Duff
2005-07-26 15:39   ` Maciej Sobczak
2005-07-26 17:45     ` Robert A Duff
replies disabled

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