* Beginner: Unchecked Deallocation
@ 2001-12-18 3:35 Antoine Lec.
2001-12-18 14:00 ` Mark Doherty
0 siblings, 1 reply; 6+ messages in thread
From: Antoine Lec. @ 2001-12-18 3:35 UTC (permalink / raw)
Hi all
I'm quite new to ADA, ( a few weeks) and I'm trying to write some code for
a monodirectional linked list.
I've done several function which satisfies me, althought I think
my style is not in the Ada phylosophy.
The first true problem i've encountered is this one: I want to destroy
my linked list, so i would like to free each link in the list.
I think that I have to use Ada.Unchecked.Deallocation()
My procedure:
procedure Free is
new Ada.Unchecked_Deallocation (Liste, Liste_Acs);
works fine when I put this code directly in the main program, but that's not
my goal; I didn't manage to put this procedure, like the other functions,
in the body and in the specification parts of my package.
When I try to do that, i got this error:
package body LCM is
|
>>> missing body for "Free" declared at lcm.ads:28
I don't understand (maybe because of misunderstanding of the Generic concept ?)
Is this procedure supposed to have a "body" ? I thought the body was provided by
the instanciation of the generic procedure ? (I'm not sure about the exact terms)
Thanks for any help or commments on my code !
(I think my code is clear enough not to translate the name of my functions into
english)
Here come my code:
/***************************************************************
* FICHIER LCM.ADS *
\***************************************************************
package LCM is
type Liste;
type Liste_Acs is access Liste;
type Liste is
record
Item : Integer;
Suiv : Liste_Acs;
end record;
Liste_Vide: exception;
function
Insere_Tete
(Ls : in Liste_Acs; Item : in Integer )
return Liste_Acs;
function
Insere_Ordre
(Ls : in Liste_Acs; Item : Integer )
return Liste_Acs;
procedure
Affiche (Ls : in Liste_Acs );
function
Supprime_Tete (Ls: in Liste_Acs)
return Liste_Acs;
procedure Free;
end LCM;
/***************************************************************
* FILE LCM.ADB *
\***************************************************************
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
package body LCM is
function Insere_Tete (
Ls : in Liste_Acs;
Item : in Integer )
return Liste_Acs is
begin
return new Liste'(Item, Ls);
end Insere_Tete;
function Insere_Ordre (
Ls : in Liste_Acs;
Item : Integer )
return Liste_Acs is
begin
if Ls=null
then
return Insere_Tete(Ls,Item);
elsif Item <= Ls.Item then
return Insere_Tete(Ls,Item);
else
return new Liste'(Ls.Item, Insere_Ordre(Ls.Suiv,Item));
end if;
end Insere_Ordre;
function Supprime_Tete (Ls: in Liste_Acs)
return Liste_Acs
is
begin
if Ls=null then raise Liste_Vide;
else return Ls.Suiv;
end if;
-- I would like to use the free() procedure Here, of course
end Supprime_Tete;
procedure Affiche (
Ls : in Liste_Acs ) is
procedure Affiche2 (
Ls : in Liste_Acs ) is
begin
if Ls/=null
then
begin
Ada.Text_IO.Put(Integer'Image(Ls.Item));
Affiche2(Ls.Suiv);
end;
end if;
end Affiche2;
begin
Ada.Text_IO.Put('(');
Affiche2(Ls);
Ada.Text_IO.Put(" )");
Ada.Text_IO.New_Line;
end Affiche;
procedure Free is
new Ada.Unchecked_Deallocation (Liste, Liste_Acs);
--gnatmake complains that there is no body here
end LCM;
--
b@v
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Beginner: Unchecked Deallocation
2001-12-18 3:35 Beginner: Unchecked Deallocation Antoine Lec.
@ 2001-12-18 14:00 ` Mark Doherty
2001-12-18 22:17 ` Antoine Lec.
0 siblings, 1 reply; 6+ messages in thread
From: Mark Doherty @ 2001-12-18 14:00 UTC (permalink / raw)
bilbo@volcanomail.com (Antoine Lec.) wrote in message news:<d442bc8e.0112171935.30eb28b1@posting.google.com>...
> My procedure:
>
> procedure Free is
> new Ada.Unchecked_Deallocation (Liste, Liste_Acs);
>
> works fine when I put this code directly in the main program, but that's not
> my goal; I didn't manage to put this procedure, like the other functions,
> in the body and in the specification parts of my package.
> When I try to do that, i got this error:
>
> package body LCM is
> |
> >>> missing body for "Free" declared at lcm.ads:28
>
> I don't understand (maybe because of misunderstanding of the Generic concept ?)
>
> Is this procedure supposed to have a "body" ? I thought the body was provided by
> the instanciation of the generic procedure ? (I'm not sure about the exact terms)
>
Procedure free declared in the spec is parameterless. The procedure
free instantiated in the body takes an in out parameter of type
liste_acs!
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Beginner: Unchecked Deallocation
2001-12-18 14:00 ` Mark Doherty
@ 2001-12-18 22:17 ` Antoine Lec.
2001-12-20 4:39 ` Patrick Hohmeyer
2001-12-20 4:54 ` Steve Doiel
0 siblings, 2 replies; 6+ messages in thread
From: Antoine Lec. @ 2001-12-18 22:17 UTC (permalink / raw)
> Procedure free declared in the spec is parameterless. The procedure
> free instantiated in the body takes an in out parameter of type
> liste_acs!
There is another problem when I change the definitions with this one :
lcm.ads:
procedure Free (LS: in out Liste_Acs);
lcm.adb
procedure Free (LS: in out Liste_Acs) is
new Ada.Unchecked_Deallocation (Liste, Liste_Acs);
This results with:
procedure Free (LS: in out Liste_Acs) is
|
>>> formal part not allowed in instantiation
So what's the problem ?
--
b@v
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Beginner: Unchecked Deallocation
2001-12-18 22:17 ` Antoine Lec.
@ 2001-12-20 4:39 ` Patrick Hohmeyer
2001-12-20 10:07 ` Antoine Lec.
2001-12-20 4:54 ` Steve Doiel
1 sibling, 1 reply; 6+ messages in thread
From: Patrick Hohmeyer @ 2001-12-20 4:39 UTC (permalink / raw)
Antoine Lec. wrote :
> > Procedure free declared in the spec is parameterless. The procedure
> > free instantiated in the body takes an in out parameter of type
> > liste_acs!
>
>
> There is another problem when I change the definitions with this one :
>
> lcm.ads:
> procedure Free (LS: in out Liste_Acs);
>
> lcm.adb
> procedure Free (LS: in out Liste_Acs) is
> new Ada.Unchecked_Deallocation (Liste, Liste_Acs);
>
> This results with:
>
> procedure Free (LS: in out Liste_Acs) is
> |
> >>> formal part not allowed in instantiation
>
> So what's the problem ?
>
You have to write
lcm.ads:
procedure Free (LS: in out Liste_Acs);
and
lcm.adb
procedure Free is new Ada.Unchecked_Deallocation (Liste, Liste_Acs);
But WHY the heck do you put Free in the ads anyway?
Do you really want the main procedure to dangle with the lists pointers?
Thumb rule : nothing that directly manipulates pointers in an ads.
Just hide it in the adb. (Unless you have a very good reason why not)
--
Patrick Hohmeyer
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Beginner: Unchecked Deallocation
2001-12-18 22:17 ` Antoine Lec.
2001-12-20 4:39 ` Patrick Hohmeyer
@ 2001-12-20 4:54 ` Steve Doiel
1 sibling, 0 replies; 6+ messages in thread
From: Steve Doiel @ 2001-12-20 4:54 UTC (permalink / raw)
If you move:
procedure Free( LS: in out List_Acs ) is
new Ada.Unchecked_Deallocation( Liste, List_Acs );
To the package spec all is well.
SteveD
"Antoine Lec." <bilbo@volcanomail.com> wrote in message
news:d442bc8e.0112181417.32159165@posting.google.com...
> > Procedure free declared in the spec is parameterless. The procedure
> > free instantiated in the body takes an in out parameter of type
> > liste_acs!
>
>
> There is another problem when I change the definitions with this one :
>
> lcm.ads:
> procedure Free (LS: in out Liste_Acs);
>
> lcm.adb
> procedure Free (LS: in out Liste_Acs) is
> new Ada.Unchecked_Deallocation (Liste, Liste_Acs);
>
> This results with:
>
> procedure Free (LS: in out Liste_Acs) is
> |
> >>> formal part not allowed in instantiation
>
> So what's the problem ?
>
> --
> b@v
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Beginner: Unchecked Deallocation
2001-12-20 4:39 ` Patrick Hohmeyer
@ 2001-12-20 10:07 ` Antoine Lec.
0 siblings, 0 replies; 6+ messages in thread
From: Antoine Lec. @ 2001-12-20 10:07 UTC (permalink / raw)
> But WHY the heck do you put Free in the ads anyway?
> Do you really want the main procedure to dangle with the lists pointers?
>
> Thumb rule : nothing that directly manipulates pointers in an ads.
> Just hide it in the adb. (Unless you have a very good reason why not)
thanks for the advice
In fact my free() procedure had to be a local procedure in the
liste_acs freeing procedure.
So it has nothing to do in the .ads
--
b@v
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2001-12-20 10:07 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-12-18 3:35 Beginner: Unchecked Deallocation Antoine Lec.
2001-12-18 14:00 ` Mark Doherty
2001-12-18 22:17 ` Antoine Lec.
2001-12-20 4:39 ` Patrick Hohmeyer
2001-12-20 10:07 ` Antoine Lec.
2001-12-20 4:54 ` Steve Doiel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox