Memory management clarification
- From: Maciej Sobczak <no.spam@xxxxxxxxxxx>
- Date: Tue, 26 Jul 2005 11:57:17 +0200
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/ .
- Follow-Ups:
- Re: Memory management clarification
- From: Robert A Duff
- Re: Memory management clarification
- From: Frank J. Lhota
- Re: Memory management clarification
- From: Adrien Plisson
- Re: Memory management clarification
- Prev by Date: Re: type and subtype
- Next by Date: Re: Help needed for ada package
- Previous by thread: Help needed for ada package
- Next by thread: Re: Memory management clarification
- Index(es):
Relevant Pages
|