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=0.7 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM,REPLYTO_WITHOUT_TO_CC autolearn=no 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-7-bit Received: by 10.68.212.232 with SMTP id nn8mr3702697pbc.1.1323919766089; Wed, 14 Dec 2011 19:29:26 -0800 (PST) Path: lh20ni23791pbb.0!nntp.google.com!news2.google.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: "Rego, P." Newsgroups: comp.lang.ada Subject: Re: Pop function Date: Wed, 14 Dec 2011 19:27:33 -0800 (PST) Organization: http://groups.google.com Message-ID: <31100824.174.1323919653265.JavaMail.geo-discussion-forums@yqcs5> References: <27517259.83.1323907586856.JavaMail.geo-discussion-forums@yqgn9> Reply-To: comp.lang.ada@googlegroups.com NNTP-Posting-Host: 189.18.155.47 Mime-Version: 1.0 X-Trace: posting.google.com 1323919765 15654 127.0.0.1 (15 Dec 2011 03:29:25 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 15 Dec 2011 03:29:25 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=189.18.155.47; posting-account=TRgI1QoAAABSsYi-ox3Pi6N-JEKKU0cu User-Agent: G2/1.0 X-Google-Web-Client: true Content-Type: text/plain; charset=ISO-8859-1 Date: 2011-12-14T19:27:33-08:00 List-Id: > that should be "constant Integer", right? Oh yes, sorry!! > 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 := 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. Ok. > 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 := 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.) Ok. So it would be like procedure Pop (Sender : in out T_List_Ptr; T_Item : out Integer) is Previous_Sender : T_List_Ptr; begin if Sender /= null then Previous_Sender := Sender; T_Item := Sender.Item; if Sender.Next /= null then Sender := T_List_Ptr (Sender.Next); end if; Free (Previous_Sender); else T_Item := 0; end if; end Pop; but in this case I could not call Some_Obj : access T_List; Other_Int : Integer; ... Other_Int := Some_Obj.Pop; because Pop is not a primitive of T_List. The notation Some_Obj.Pop would be just sintatic sugar, but would exist a way we can implement Pop for using with this? (but sure...that's also ok for the non-sugar case)