comp.lang.ada
 help / color / mirror / Atom feed
* Unchecked deallocation question
@ 2004-11-24 15:38 Alex R. Mosteo
  2004-11-24 17:24 ` Jeffrey Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Alex R. Mosteo @ 2004-11-24 15:38 UTC (permalink / raw)


I'm wondering if this is correct:

type Root is tagged record ...

type Root_Access is access Root;

type Derived is new Root with ...

type Derived_Access is access Derived;

procedure Free is
    new Ada.Unchecked_Deallocation (Root'Class, Root_Access);

declare
    It : Derived_Access := new Derived;
begin
    Free (Root_Access (It)); -- <-- THIS DEALLOCATION I'M ASKING
end;

Assuming that all involved access types use the same Storage_Pool.

The purpose is to have a single deallocator in the root package and to 
not have to instantiate a lot of deallocators for derived types.



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

* Re: Unchecked deallocation question
  2004-11-24 15:38 Unchecked deallocation question Alex R. Mosteo
@ 2004-11-24 17:24 ` Jeffrey Carter
  2004-11-24 17:35   ` Alex R. Mosteo
  2004-11-24 17:33 ` David Botton
  2004-11-25  8:35 ` Martin Krischik
  2 siblings, 1 reply; 12+ messages in thread
From: Jeffrey Carter @ 2004-11-24 17:24 UTC (permalink / raw)


Alex R. Mosteo wrote:

> type Root is tagged record ...
> 
> type Root_Access is access Root;
> 
> type Derived is new Root with ...
> 
> type Derived_Access is access Derived;
> 
> procedure Free is
>    new Ada.Unchecked_Deallocation (Root'Class, Root_Access);

Root_Access designates Root, not Root'Class.

> 
> declare
>    It : Derived_Access := new Derived;
> begin
>    Free (Root_Access (It)); -- <-- THIS DEALLOCATION I'M ASKING
> end;
> 
> The purpose is to have a single deallocator in the root package and to 
> not have to instantiate a lot of deallocators for derived types.

You'd be better off having

type Root_Access is access Root'Class;

procedure Free is new ...

...

It : Root_Access := new Derived;

...

Free (It);

But you'd really be better off not using access types, or encapsulating 
and hiding their use if that's not possible.

-- 
Jeff Carter
"My name is Jim, but most people call me ... Jim."
Blazing Saddles
39




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

* Re: Unchecked deallocation question
  2004-11-24 15:38 Unchecked deallocation question Alex R. Mosteo
  2004-11-24 17:24 ` Jeffrey Carter
@ 2004-11-24 17:33 ` David Botton
  2004-11-25  8:35 ` Martin Krischik
  2 siblings, 0 replies; 12+ messages in thread
From: David Botton @ 2004-11-24 17:33 UTC (permalink / raw)


Your answer is in the Ada Source Code Treasury - Take a look at:

http://www.adapower.com/index.php?Command=Class&ClassID=Basics&CID=159
David Botton




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

* Re: Unchecked deallocation question
  2004-11-24 17:24 ` Jeffrey Carter
@ 2004-11-24 17:35   ` Alex R. Mosteo
  0 siblings, 0 replies; 12+ messages in thread
From: Alex R. Mosteo @ 2004-11-24 17:35 UTC (permalink / raw)


Jeffrey Carter wrote:

> You'd be better off having
> 
> type Root_Access is access Root'Class;

Uuuups, that's correct. That's what I wanted to ask.



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

* Re: Unchecked deallocation question
  2004-11-24 15:38 Unchecked deallocation question Alex R. Mosteo
  2004-11-24 17:24 ` Jeffrey Carter
  2004-11-24 17:33 ` David Botton
@ 2004-11-25  8:35 ` Martin Krischik
  2004-11-25  9:12   ` Alex R. Mosteo
  2004-11-25 23:24   ` Adrien Plisson
  2 siblings, 2 replies; 12+ messages in thread
From: Martin Krischik @ 2004-11-25  8:35 UTC (permalink / raw)


Alex R. Mosteo wrote:

> declare
>     It : Derived_Access := new Derived;
> begin
>     Free (Root_Access (It)); -- <-- THIS DEALLOCATION I'M ASKING
> end;

You should carefully thing if you really need it. Unlike C/C++ the following
is actually valid:

declare
    It : Root'Class := Derived'(...);
begin
    ...
end;

It is more or less the same concept as:

declare
    It : String := "Hello World!";
begin
    ...
end;

3 Month into learning Ada I began to fully understand what an "indefinte
type" is and what it can do fro you - and then I remove more then 80% of
all my access types.

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



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

* Re: Unchecked deallocation question
  2004-11-25  8:35 ` Martin Krischik
@ 2004-11-25  9:12   ` Alex R. Mosteo
  2004-11-25 23:24   ` Adrien Plisson
  1 sibling, 0 replies; 12+ messages in thread
From: Alex R. Mosteo @ 2004-11-25  9:12 UTC (permalink / raw)


Martin Krischik wrote:
> Alex R. Mosteo wrote:
> 
> 
>>declare
>>    It : Derived_Access := new Derived;
>>begin
>>    Free (Root_Access (It)); -- <-- THIS DEALLOCATION I'M ASKING
>>end;
> 
> 
> You should carefully thing if you really need it. Unlike C/C++ the following
> is actually valid:
> 
> declare
>     It : Root'Class := Derived'(...);
> begin
>     ...
> end;
> 
> It is more or less the same concept as:
> 
> declare
>     It : String := "Hello World!";
> begin
>     ...
> end;
> 
> 3 Month into learning Ada I began to fully understand what an "indefinte
> type" is and what it can do fro you - and then I remove more then 80% of
> all my access types.

Ok, I see I've worded my question poorly indeed. I know these concepts 
and my example code was just a quick improvisation. I was just concerned 
about the feasibility of using a 'Class deallocator to deallocate an 
entire polimorphic list, for example. In any case, it was a doubt that 
arised in a bad moment of mind, because I've indeed done that several 
times. There was some oddity in what was coding that threw me off-track 
for some time.

Thanks for your responses,

A.



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

* Re: Unchecked deallocation question
  2004-11-25  8:35 ` Martin Krischik
  2004-11-25  9:12   ` Alex R. Mosteo
@ 2004-11-25 23:24   ` Adrien Plisson
  2004-11-26  4:05     ` Jim Rogers
  2004-11-26  9:06     ` Dmitry A. Kazakov
  1 sibling, 2 replies; 12+ messages in thread
From: Adrien Plisson @ 2004-11-25 23:24 UTC (permalink / raw)


Martin Krischik wrote:
> You should carefully thing if you really need it. Unlike C/C++ the following
> is actually valid:
> 
> declare
>     It : Root'Class := Derived'(...);
> begin
>     ...
> end;

mmmm, i don't think there is any problem doing this in C++:

class Root;
class Derived : public Root;

Root &It = Derived( ... );

it just uses a reference type which is almost the same as ada access 
type but you cannot assign null to it and cannot dereference it.

-- 
rien




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

* Re: Unchecked deallocation question
  2004-11-25 23:24   ` Adrien Plisson
@ 2004-11-26  4:05     ` Jim Rogers
  2004-11-26 10:53       ` Sergey
  2004-11-26  9:06     ` Dmitry A. Kazakov
  1 sibling, 1 reply; 12+ messages in thread
From: Jim Rogers @ 2004-11-26  4:05 UTC (permalink / raw)


Adrien Plisson <aplisson-news@stochastique.net> wrote in
news:41a66926$0$25070$ba620e4c@news.skynet.be: 

> Martin Krischik wrote:
>> You should carefully thing if you really need it. Unlike C/C++ the
>> following is actually valid:
>> 
>> declare
>>     It : Root'Class := Derived'(...);
>> begin
>>     ...
>> end;
> 
> mmmm, i don't think there is any problem doing this in C++:
> 
> class Root;
> class Derived : public Root;
> 
> Root &It = Derived( ... );
> 
> it just uses a reference type which is almost the same as ada access 
> type but you cannot assign null to it and cannot dereference it.
> 

This is a difference between C++ and Ada. The C++ solution uses
dynamic memory allocation (from the free space or heap) while the
Ada solution uses the stack. Use of the stack eliminates the need
for explicit allocation and deallocation.

Jim Rogers




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

* Re: Unchecked deallocation question
  2004-11-25 23:24   ` Adrien Plisson
  2004-11-26  4:05     ` Jim Rogers
@ 2004-11-26  9:06     ` Dmitry A. Kazakov
  2004-11-26 18:20       ` Sergey
  1 sibling, 1 reply; 12+ messages in thread
From: Dmitry A. Kazakov @ 2004-11-26  9:06 UTC (permalink / raw)


On Fri, 26 Nov 2004 00:24:15 +0100, Adrien Plisson wrote:

> Martin Krischik wrote:
>> You should carefully thing if you really need it. Unlike C/C++ the following
>> is actually valid:
>> 
>> declare
>>     It : Root'Class := Derived'(...);
>> begin
>>     ...
>> end;
> 
> mmmm, i don't think there is any problem doing this in C++:
> 
> class Root;
> class Derived : public Root;
> 
> Root &It = Derived( ... );
> 
> it just uses a reference type which is almost the same as ada access 
> type but you cannot assign null to it and cannot dereference it.

Right, the difference appears when Derived is created by a factory:

declare
   It : Root'Class := Create (...);
begin

Here Create returns Root'Class, so the actual type is unknown until
run-time. In C++ this is only possible using heap and pointers. In Ada It
will be indeed allocated on the stack.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Unchecked deallocation question
  2004-11-26  4:05     ` Jim Rogers
@ 2004-11-26 10:53       ` Sergey
  0 siblings, 0 replies; 12+ messages in thread
From: Sergey @ 2004-11-26 10:53 UTC (permalink / raw)


Jim Rogers <jimmaureenrogers@att.net> wrote in message news:<OXxpd.59698$7i4.57298@bgtnsc05-news.ops.worldnet.att.net>...
> Adrien Plisson <aplisson-news@stochastique.net> wrote in
> news:41a66926$0$25070$ba620e4c@news.skynet.be: 
> 
> > Martin Krischik wrote:
> >> You should carefully thing if you really need it. Unlike C/C++ the
> >> following is actually valid:
> >> 
> >> declare
> >>     It : Root'Class := Derived'(...);
> >> begin
> >>     ...
> >> end;
> > 
> > mmmm, i don't think there is any problem doing this in C++:
> > 
> > class Root;
> > class Derived : public Root;
> > 
> > Root &It = Derived( ... );
> > 
> > it just uses a reference type which is almost the same as ada access 
> > type but you cannot assign null to it and cannot dereference it.
> > 
> 
> This is a difference between C++ and Ada. The C++ solution uses
> dynamic memory allocation (from the free space or heap) while the
> Ada solution uses the stack. 

An example was posted by Adrien has nothing to do with dynamic memory
allocation. As you can see, Derived is allocated from the STACK.

>Use of the stack eliminates the need
> for explicit allocation and deallocation.
> 
> Jim Rogers



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

* Re: Unchecked deallocation question
  2004-11-26  9:06     ` Dmitry A. Kazakov
@ 2004-11-26 18:20       ` Sergey
  2004-11-27  8:43         ` Dmitry A. Kazakov
  0 siblings, 1 reply; 12+ messages in thread
From: Sergey @ 2004-11-26 18:20 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message news:<hzl9ywp1t6xh$.6ob5swxlfy6e.dlg@40tude.net>...
> On Fri, 26 Nov 2004 00:24:15 +0100, Adrien Plisson wrote:
> 
> > Martin Krischik wrote:
> >> You should carefully thing if you really need it. Unlike C/C++ the following
> >> is actually valid:
> >> 
> >> declare
> >>     It : Root'Class := Derived'(...);
> >> begin
> >>     ...
> >> end;
> > 
> > mmmm, i don't think there is any problem doing this in C++:
> > 
> > class Root;
> > class Derived : public Root;
> > 
> > Root &It = Derived( ... );
> > 
> > it just uses a reference type which is almost the same as ada access 
> > type but you cannot assign null to it and cannot dereference it.
> 
> Right, the difference appears when Derived is created by a factory:
> 
> declare
>    It : Root'Class := Create (...);
> begin
> 
> Here Create returns Root'Class, so the actual type is unknown until
> run-time. In C++ this is only possible using heap and pointers. In Ada It
> will be indeed allocated on the stack.

Nyet! You're wrong here. The example posted by Adrien is perfectly
right.

const Root &It = Derived( ... );

Derived gets allocated from the stack... But what you'll do with
constant the constant refence is another story...



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

* Re: Unchecked deallocation question
  2004-11-26 18:20       ` Sergey
@ 2004-11-27  8:43         ` Dmitry A. Kazakov
  0 siblings, 0 replies; 12+ messages in thread
From: Dmitry A. Kazakov @ 2004-11-27  8:43 UTC (permalink / raw)


On 26 Nov 2004 10:20:01 -0800, Sergey wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message news:<hzl9ywp1t6xh$.6ob5swxlfy6e.dlg@40tude.net>...
>> On Fri, 26 Nov 2004 00:24:15 +0100, Adrien Plisson wrote:
>> 
>>> Martin Krischik wrote:
>>>> You should carefully thing if you really need it. Unlike C/C++ the following
>>>> is actually valid:
>>>> 
>>>> declare
>>>>     It : Root'Class := Derived'(...);
>>>> begin
>>>>     ...
>>>> end;
>>> 
>>> mmmm, i don't think there is any problem doing this in C++:
>>> 
>>> class Root;
>>> class Derived : public Root;
>>> 
>>> Root &It = Derived( ... );
>>> 
>>> it just uses a reference type which is almost the same as ada access 
>>> type but you cannot assign null to it and cannot dereference it.
>> 
>> Right, the difference appears when Derived is created by a factory:
>> 
>> declare
>>    It : Root'Class := Create (...);
>> begin
>> 
>> Here Create returns Root'Class, so the actual type is unknown until
>> run-time. In C++ this is only possible using heap and pointers. In Ada It
>> will be indeed allocated on the stack.
> 
> Nyet! You're wrong here. The example posted by Adrien is perfectly
> right.
> 
> const Root &It = Derived( ... );
> 
> Derived gets allocated from the stack... But what you'll do with
> constant the constant refence is another story...

When the type is statically known then there is no reason to declare the
object as something else. The example makes no sense.

Note that the point was that pointers are dangerous and in Ada there is
less need in them as compared to C++, that is what Martin wished to
demonstrate, I presume.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

end of thread, other threads:[~2004-11-27  8:43 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-24 15:38 Unchecked deallocation question Alex R. Mosteo
2004-11-24 17:24 ` Jeffrey Carter
2004-11-24 17:35   ` Alex R. Mosteo
2004-11-24 17:33 ` David Botton
2004-11-25  8:35 ` Martin Krischik
2004-11-25  9:12   ` Alex R. Mosteo
2004-11-25 23:24   ` Adrien Plisson
2004-11-26  4:05     ` Jim Rogers
2004-11-26 10:53       ` Sergey
2004-11-26  9:06     ` Dmitry A. Kazakov
2004-11-26 18:20       ` Sergey
2004-11-27  8:43         ` Dmitry A. Kazakov

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