From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,75f02dbbddbbdc88 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Received: by 10.68.73.229 with SMTP id o5mr3539208pbv.7.1323914775489; Wed, 14 Dec 2011 18:06:15 -0800 (PST) Path: lh20ni23582pbb.0!nntp.google.com!news1.google.com!postnews.google.com!d17g2000yql.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Pop function Date: Wed, 14 Dec 2011 18:06:15 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <27517259.83.1323907586856.JavaMail.geo-discussion-forums@yqgn9> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 X-Trace: posting.google.com 1323914775 30193 127.0.0.1 (15 Dec 2011 02:06:15 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 15 Dec 2011 02:06:15 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: d17g2000yql.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: ARLUEHNKC X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2011-12-14T18:06:15-08:00 List-Id: On Dec 14, 4:06=A0pm, "Rego, P." wrote: > Hello, > > Given a list type > =A0 =A0type T_List is > =A0 =A0 =A0 record > =A0 =A0 =A0 =A0 =A0Next : access T_List; > =A0 =A0 =A0 =A0 =A0Item : Integer; > =A0 =A0 =A0 end record; > =A0 =A0T_List_Ptr is access T_List; > > Is it right to implement a pop function like the following? (Free is an U= nchecked_Deallocation) > =A0 =A0function Pop (Sender : access T_List) return Integer is > =A0 =A0 =A0 Current_Sender_Ptr : T_List_Ptr :=3D T_List_Ptr (Sender); > =A0 =A0 =A0 Current_Item : constant T_List :=3D Sender.Item; that should be "constant Integer", right? > =A0 =A0begin > =A0 =A0 =A0 if Sender /=3D null then > =A0 =A0 =A0 =A0 =A0if Sender.Next /=3D null then > =A0 =A0 =A0 =A0 =A0 =A0 Current_Sender_Ptr :=3D T_List_Ptr (Current_Sende= r_Ptr.Next); > =A0 =A0 =A0 =A0 =A0end if; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Free (Current_Sender_Ptr); > =A0 =A0 =A0 =A0 =A0return Current_Item; > =A0 =A0 =A0 else > =A0 =A0 =A0 =A0 =A0return 0; > =A0 =A0 =A0 end if; > =A0 =A0end Pop; > > I mean, if I set Current_Sender_Ptr :=3D T_List_Ptr (Current_Sender_Ptr.N= ext), it's > equivalent to Sender :=3D Sender.Next? No. Current_Sender_Ptr is a local variable. When you set Current_Sender_Ptr, you're changing the value of the local variable, but that does not affect *anything* outside the Pop procedure. Free (Current_Sender_Ptr) will then set Current_Sender_Ptr to null. Then Pop will return and Current_Sender_Ptr will go away since it's a local variable, so the work you've done of setting it to the "Next" value will be lost. If you call N :=3D Pop (Sender); assuming that Sender points to the head of the list, this call won't change Sender at all. After the call, Sender will point to an item that has been deallocated, which is very, very bad. What you're probably missing is that Sender is treated as an IN parameter here, so it can't be modified. You probably want to make it an IN OUT parameter, which means that Pop can't be a function (up through Ada 2005; I think that rule is changing in Ada 2012). So I'd do something like procedure Pop (List_Head : in out T_List_Ptr; Item : out Integer) is ... and then in Pop, at some point, List_Head :=3D List_Head.Next. Plus you'll need to rearrange the code so that you don't try to access Sender.Item before checking to make sure Sender isn't null, as Martin pointed out. (Also, it wouldn't be my style to reserve a special Integer value to indicate an empty list. It might be OK in some applications, but it makes the code that calls Pop more difficult to follow.) -- Adam