comp.lang.ada
 help / color / mirror / Atom feed
* dinamic object reclamation
@ 2001-09-19 14:27 Anisimkov
  2001-09-19 14:55 ` Ted Dennison
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Anisimkov @ 2001-09-19 14:27 UTC (permalink / raw)


I wrote the test program about it.
-----------------------------
with text_io; use text_io;
procedure alloc is

  dummy : character;

  procedure act (i : integer)
  is
      type block is array (1 .. 1024) of integer;
      type block_ptr is access all block;
      ptr : block_ptr;
  begin
      ptr := new block;
      ptr (ptr'LAst) := i;
      put (integer'image(I));
  end;

begin
  for i in 1..20000 loop
    act (i);
    get_immediate(dummy);
  end loop;
end;
--------------------
The question is:
Is memory allocated by
      ptr := new block;
have to be freed automatically after leaving of procedure act ?
Am I have to use Ada.Unchecked_Deallocation for free memory ?
I'm acking becouse there is a memory leak without Unchecked_Deallocation
when program compiled in the GNAT 3.13.
Is it bug or regular behavior ?







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

* Re: dinamic object reclamation
  2001-09-19 14:27 dinamic object reclamation Anisimkov
@ 2001-09-19 14:55 ` Ted Dennison
  2001-09-19 17:44   ` Anisimkov
  2001-09-19 15:02 ` David C. Hoos
  2001-09-20  2:43 ` David Botton
  2 siblings, 1 reply; 14+ messages in thread
From: Ted Dennison @ 2001-09-19 14:55 UTC (permalink / raw)


In article <9oa69d$6ej$1@ns.omskelecom.ru>, Anisimkov says...
>  procedure act (i : integer)
>  is
>      type block is array (1 .. 1024) of integer;
>      type block_ptr is access all block;
>      ptr : block_ptr;
>  begin
>      ptr := new block;
>      ptr (ptr'LAst) := i;
>      put (integer'image(I));
>  end;
>Is memory allocated by
>      ptr := new block;
>have to be freed automatically after leaving of procedure act ?

I believe compilers are allowed, but not required, to garbage collect here. If
you want to force the issue w/o using Unchecked_Deallocation, specify a fixed
size for Block_Ptr's storage pool. For example, in the above code, add the line:

for Block_Ptr'Storage_Size use 4098;

and you should be OK (If you get Storage_Error, jack up the number a bit). You
won't be able to allocate any more than the specified number of bytes this way,
but a conforming compiler *will* reclaim the storage when the access type goes
out of scope. (LRM 13.11-18).


---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: dinamic object reclamation
  2001-09-19 14:27 dinamic object reclamation Anisimkov
  2001-09-19 14:55 ` Ted Dennison
@ 2001-09-19 15:02 ` David C. Hoos
  2001-09-19 19:28   ` Anisimkov
  2001-09-20  2:43 ` David Botton
  2 siblings, 1 reply; 14+ messages in thread
From: David C. Hoos @ 2001-09-19 15:02 UTC (permalink / raw)
  To: comp.lang.ada

Yes, you have to use Ada.Unchecked_Deallocation to free the memory.

Types derived from Ada.Finalization.Controlled can have their memory
automatically freed when execution leaves the scope in which it was
declared.

----- Original Message ----- 
From: "Anisimkov" <anisimkov@yahoo.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Wednesday, September 19, 2001 9:27 AM
Subject: dinamic object reclamation


> I wrote the test program about it.
> -----------------------------
> with text_io; use text_io;
> procedure alloc is
> 
>   dummy : character;
> 
>   procedure act (i : integer)
>   is
>       type block is array (1 .. 1024) of integer;
>       type block_ptr is access all block;
>       ptr : block_ptr;
>   begin
>       ptr := new block;
>       ptr (ptr'LAst) := i;
>       put (integer'image(I));
>   end;
> 
> begin
>   for i in 1..20000 loop
>     act (i);
>     get_immediate(dummy);
>   end loop;
> end;
> --------------------
> The question is:
> Is memory allocated by
>       ptr := new block;
> have to be freed automatically after leaving of procedure act ?
> Am I have to use Ada.Unchecked_Deallocation for free memory ?
> I'm acking becouse there is a memory leak without Unchecked_Deallocation
> when program compiled in the GNAT 3.13.
> Is it bug or regular behavior ?
> 
> 
> 
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 




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

* Re: dinamic object reclamation
  2001-09-19 17:44   ` Anisimkov
@ 2001-09-19 17:15     ` Ted Dennison
  2001-09-19 19:21       ` Anisimkov
  0 siblings, 1 reply; 14+ messages in thread
From: Ted Dennison @ 2001-09-19 17:15 UTC (permalink / raw)


In article <9oahqo$3on$1@ns.omskelecom.ru>, Anisimkov says...
>
>But there is more then limit of memory usage
>becouse I wrote
>      for Block_Ptr'Storage_Size use Integer'Last;
>inside of procedure "act".
>
>and memory is not leaking now.

Interesting. I'm curious exactly what Gnat is doing there. I would have thought
that it would just try to advance the stack pointer by that much, but clearly it
isn't doing that (2GB? I think not!). Perhaps its doing something nice, like
saying "that's rediculous! I'll just give him X". 

I'd still highly encourage you to find a reasonable max size, rather than using
Integer'last. It could be that virtual memory is saving you here, but it could
also be that you are depriving yourself of a large amount of memory that you
could be using elsewhere.

>It looks like a trick.

Well, it is. But it is clearly one that was placed into the language on purpose.
:-)

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: dinamic object reclamation
  2001-09-19 14:55 ` Ted Dennison
@ 2001-09-19 17:44   ` Anisimkov
  2001-09-19 17:15     ` Ted Dennison
  0 siblings, 1 reply; 14+ messages in thread
From: Anisimkov @ 2001-09-19 17:44 UTC (permalink / raw)


> >  procedure act (i : integer)
> >  is
> >      type block is array (1 .. 1024) of integer;
> >      type block_ptr is access all block;
> >      ptr : block_ptr;
> >  begin
> >      ptr := new block;
> >      ptr (ptr'LAst) := i;
> >      put (integer'image(I));
> >  end;

> size for Block_Ptr's storage pool. For example, in the above code, add the
line:
>
> for Block_Ptr'Storage_Size use 4098;
>
> and you should be OK (If you get Storage_Error, jack up the number a bit).
You
> won't be able to allocate any more than the specified number of bytes this
way,
> but a conforming compiler *will* reclaim the storage when the access type
goes
> out of scope. (LRM 13.11-18).

Yes it is.

Thank you Ted.

But there is more then limit of memory usage
becouse I wrote
      for Block_Ptr'Storage_Size use Integer'Last;
inside of procedure "act".

and memory is not leaking now.

It looks like a trick.





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

* Re: dinamic object reclamation
  2001-09-19 19:21       ` Anisimkov
@ 2001-09-19 19:18         ` Ted Dennison
  2001-09-19 19:51         ` Simon Wright
  1 sibling, 0 replies; 14+ messages in thread
From: Ted Dennison @ 2001-09-19 19:18 UTC (permalink / raw)


In article <9oangi$dp8$1@ns.omskelecom.ru>, Anisimkov says...
>
>If the Ada.Unchecked_Deallocation is Unchecked
>what deallocation is "Checked" ?

None, unless you make one yourself. The name graphicly emphasises the point that
this is an dangerous operation, and you could be hosing yourself if you aren't
careful (eg: Deallocating memory that you will need later, deallocating memory
that has already been deallocated, deallocating memory on the stack, etc). Ada
(unlike many lesser languages) worries about safety, and performing your own
manual deallocations is inherently unsafe.

As a matter of fact, the entire language is organized in a way to greatly reduce
your need for dynamic allocation. If you are an Ada newbie and find yourself
wanting to do dynamic allocation, you are probably doing something *wrong*.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: dinamic object reclamation
  2001-09-19 17:15     ` Ted Dennison
@ 2001-09-19 19:21       ` Anisimkov
  2001-09-19 19:18         ` Ted Dennison
  2001-09-19 19:51         ` Simon Wright
  0 siblings, 2 replies; 14+ messages in thread
From: Anisimkov @ 2001-09-19 19:21 UTC (permalink / raw)


> >But there is more then limit of memory usage
> >becouse I wrote
> >      for Block_Ptr'Storage_Size use Integer'Last;
> >inside of procedure "act".
> >
> >and memory is not leaking now.
>
> >It looks like a trick.
>
> Well, it is. But it is clearly one that was placed into the language on
purpose.
> :-)

The one reasonable explanation i can imagine is
that when we declearing
      for Block_Ptr'Storage_Size use Integer'Last;
GNAT creates storage_pool locally in the "act" procedure.
and when we are leaving this procedure
storage pool Finalized.

Could be nice to have standard way to declate
access type with a local storage pool.

If the Ada.Unchecked_Deallocation is Unchecked
what deallocation is "Checked" ?





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

* Re: dinamic object reclamation
  2001-09-19 19:28   ` Anisimkov
@ 2001-09-19 19:21     ` David C. Hoos
  2001-09-19 21:47     ` Stephen Leake
  1 sibling, 0 replies; 14+ messages in thread
From: David C. Hoos @ 2001-09-19 19:21 UTC (permalink / raw)
  To: comp.lang.ada


----- Original Message ----- 
From: "Anisimkov" <anisimkov@yahoo.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Wednesday, September 19, 2001 2:28 PM
Subject: Re: dinamic object reclamation


> > Types derived from Ada.Finalization.Controlled can have their memory
> > automatically freed when execution leaves the scope in which it was
> > declared.
> 
> Anyway we have to call Ada.Unchecked_Deallocation
> inside of overloaded Finalize procedure
> for memory allocated inside of Initialize procedure.
> 
True. But controlled types need to be declared at library level, and
you don't have to remember to call Unchecked_Deallocation for
every instance of an object of the type.

> Where is a "Checked" deallocation ?

There is none.

> 
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 




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

* Re: dinamic object reclamation
  2001-09-19 15:02 ` David C. Hoos
@ 2001-09-19 19:28   ` Anisimkov
  2001-09-19 19:21     ` David C. Hoos
  2001-09-19 21:47     ` Stephen Leake
  0 siblings, 2 replies; 14+ messages in thread
From: Anisimkov @ 2001-09-19 19:28 UTC (permalink / raw)


> Types derived from Ada.Finalization.Controlled can have their memory
> automatically freed when execution leaves the scope in which it was
> declared.

Anyway we have to call Ada.Unchecked_Deallocation
inside of overloaded Finalize procedure
for memory allocated inside of Initialize procedure.

Where is a "Checked" deallocation ?





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

* Re: dinamic object reclamation
  2001-09-19 19:21       ` Anisimkov
  2001-09-19 19:18         ` Ted Dennison
@ 2001-09-19 19:51         ` Simon Wright
  1 sibling, 0 replies; 14+ messages in thread
From: Simon Wright @ 2001-09-19 19:51 UTC (permalink / raw)


"Anisimkov" <anisimkov@yahoo.com> writes:

> > >But there is more then limit of memory usage
> > >becouse I wrote
> > >      for Block_Ptr'Storage_Size use Integer'Last;
> > >inside of procedure "act".
> > >
> > >and memory is not leaking now.
> >
> > >It looks like a trick.
> >
> > Well, it is. But it is clearly one that was placed into the language on
> purpose.
> > :-)
> 
> The one reasonable explanation i can imagine is
> that when we declearing
>       for Block_Ptr'Storage_Size use Integer'Last;
> GNAT creates storage_pool locally in the "act" procedure.
> and when we are leaving this procedure
> storage pool Finalized.

Exactly so.

> Could be nice to have standard way to declate
> access type with a local storage pool.

I rather think that this is it!



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

* Re: dinamic object reclamation
  2001-09-19 19:28   ` Anisimkov
  2001-09-19 19:21     ` David C. Hoos
@ 2001-09-19 21:47     ` Stephen Leake
  1 sibling, 0 replies; 14+ messages in thread
From: Stephen Leake @ 2001-09-19 21:47 UTC (permalink / raw)


"Anisimkov" <anisimkov@yahoo.com> writes:

> > Types derived from Ada.Finalization.Controlled can have their memory
> > automatically freed when execution leaves the scope in which it was
> > declared.
> 
> Anyway we have to call Ada.Unchecked_Deallocation
> inside of overloaded Finalize procedure
> for memory allocated inside of Initialize procedure.
> 
> Where is a "Checked" deallocation ?

There is no "Checked" deallocation, because the compiler cannot
guarantee that you have not copied the pointer, thus it cannot
guarantee that the deallocation is "safe".

You can implement your own abstract data type with reference counting,
or some other scheme, but the compiler won't do it for you.

-- 
-- Stephe



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

* Re: dinamic object reclamation
  2001-09-19 14:27 dinamic object reclamation Anisimkov
  2001-09-19 14:55 ` Ted Dennison
  2001-09-19 15:02 ` David C. Hoos
@ 2001-09-20  2:43 ` David Botton
  2001-09-20  7:39   ` Larry Kilgallen
  2 siblings, 1 reply; 14+ messages in thread
From: David Botton @ 2001-09-20  2:43 UTC (permalink / raw)
  To: comp.lang.ada

Take a look on AdaPower in general at http://www.adapower.com/lang and in
particular at:

http://www.adapower.com/lang/storage_size.html

David Botton


----- Original Message -----
From: "Anisimkov" <anisimkov@yahoo.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Wednesday, September 19, 2001 10:27 AM
Subject: dinamic object reclamation


> I wrote the test program about it.
> -----------------------------
> with text_io; use text_io;
> procedure alloc is
>
>   dummy : character;
>
>   procedure act (i : integer)
>   is
>       type block is array (1 .. 1024) of integer;
>       type block_ptr is access all block;
>       ptr : block_ptr;
>   begin
>       ptr := new block;
>       ptr (ptr'LAst) := i;
>       put (integer'image(I));
>   end;
>
> begin
>   for i in 1..20000 loop
>     act (i);
>     get_immediate(dummy);
>   end loop;
> end;
> --------------------
> The question is:
> Is memory allocated by
>       ptr := new block;
> have to be freed automatically after leaving of procedure act ?
> Am I have to use Ada.Unchecked_Deallocation for free memory ?
> I'm acking becouse there is a memory leak without Unchecked_Deallocation
> when program compiled in the GNAT 3.13.
> Is it bug or regular behavior ?
>
>
>
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




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

* Re: dinamic object reclamation
  2001-09-20  2:43 ` David Botton
@ 2001-09-20  7:39   ` Larry Kilgallen
  2001-09-20 13:21     ` Ted Dennison
  0 siblings, 1 reply; 14+ messages in thread
From: Larry Kilgallen @ 2001-09-20  7:39 UTC (permalink / raw)


I have been keeping my mouth shut and ignoring this thread,
figuring I did not know enough computer science to understand
that strange term "dinamic".

Now I get it :-)



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

* Re: dinamic object reclamation
  2001-09-20  7:39   ` Larry Kilgallen
@ 2001-09-20 13:21     ` Ted Dennison
  0 siblings, 0 replies; 14+ messages in thread
From: Ted Dennison @ 2001-09-20 13:21 UTC (permalink / raw)


In article <UbWBgGMm2f6B@eisner.encompasserve.org>, Larry Kilgallen says...
>
>I have been keeping my mouth shut and ignoring this thread,
>figuring I did not know enough computer science to understand
>that strange term "dinamic".
>
>Now I get it :-)

I'm not sure if I should be proud or ashamed that I speak "Misspelling" more
fluently than you. :-)

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

end of thread, other threads:[~2001-09-20 13:21 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-19 14:27 dinamic object reclamation Anisimkov
2001-09-19 14:55 ` Ted Dennison
2001-09-19 17:44   ` Anisimkov
2001-09-19 17:15     ` Ted Dennison
2001-09-19 19:21       ` Anisimkov
2001-09-19 19:18         ` Ted Dennison
2001-09-19 19:51         ` Simon Wright
2001-09-19 15:02 ` David C. Hoos
2001-09-19 19:28   ` Anisimkov
2001-09-19 19:21     ` David C. Hoos
2001-09-19 21:47     ` Stephen Leake
2001-09-20  2:43 ` David Botton
2001-09-20  7:39   ` Larry Kilgallen
2001-09-20 13:21     ` Ted Dennison

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