help / color / mirror / Atom feed
* Unchecked_Deallocation with tagged class type.
@ 2023-11-14 21:11 Blady
  2023-11-14 22:42 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 7+ messages in thread
From: Blady @ 2023-11-14 21:11 UTC (permalink / raw)


The following code present a Finalize procedure with a parameter of 
access tagged class type in order to deallocate the memory of the given 
parameter from the root tagged type TTA and his children.
The same for TTB which is inherited from TTA. But this Finalize call 
Finalize of TTA.
It may be not the best idea.
But let's see:

with Ada.Unchecked_Deallocation;
procedure test_20231113_free_tag is

    type TTA is tagged record
       AA : Integer;
    end record;
    type ATTA is access all TTA;
    type CATTA is access all TTA'Class;
    procedure Finalize (O : in out CATTA) is
       procedure Free is new Ada.Unchecked_Deallocation (TTA, ATTA);
       Free (ATTA (O));
    end Finalize;

    type TTB is new TTA with record
       BB : Integer;
    end record;
    type ATTB is access all TTB;
    type CATTB is access all TTB'Class;
    procedure Finalize (O : in out CATTB) is
       Finalize (CATTA (O));
    end Finalize;

    OA : CATTA := new TTA;
    OB : CATTB := new TTB;

    Finalize (OA);
    Finalize (OB);
end test_20231113_free_tag;

The procedure Free is the instanciation of Unchecked_Deallocation with 
the tagged type TTA.
Thus the call "Finalize (OA);" deallocate the memory of object OA of 
type access class TTA.

But what does "Finalize (OB);"?
What is the memory deallocate of object OB of type TTB?

Thanks, Pascal.

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

end of thread, other threads:[~2023-11-17 13:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-14 21:11 Unchecked_Deallocation with tagged class type Blady
2023-11-14 22:42 ` Dmitry A. Kazakov
2023-11-15 20:26   ` Blady
2023-11-15 21:17     ` Dmitry A. Kazakov
2023-11-16 20:29       ` Blady
2023-11-17 13:12         ` AdaMagica
2023-11-17 13:13           ` AdaMagica

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