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,5e53057e86953953 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local02.nntp.dca.giganews.com!nntp.telenor.com!news.telenor.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 17 Jun 2008 07:03:55 -0500 From: Reinert Korsnes Subject: Re: Question on initialization of packages Newsgroups: comp.lang.ada Date: Tue, 17 Jun 2008 14:03:55 +0200 References: <1pok6brk3yyxf$.ct5gwnf4g97p$.dlg@40tude.net> User-Agent: KNode/0.10.4 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit Message-ID: X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-CHZTRlqlU0lnhf/rGGI4Pe/3wCehIxllleu4sBgoUzfRujqktfYD6TS5bUaJziFR9iq9X2Hj4fJKU9i!yy4K+vBxQkS0mvqTg3uUCGVzEyujDksLM6iNevia9TBU2ERuc4dv1YgUjlweZAa9AQsSoOq8F9g= X-Complaints-To: news-abuse@telenor.net X-DMCA-Complaints-To: news-abuse@telenor.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.39 Xref: g2news1.google.com comp.lang.ada:739 Date: 2008-06-17T14:03:55+02:00 List-Id: First: many thanks for comments. It gave me a nice learning curve today :-) Dmitry A. Kazakov wrote: > On Tue, 17 Jun 2008 11:14:59 +0200, Reinert Korsnes wrote: > >> Dmitry A. Kazakov wrote: >> >>> On Tue, 17 Jun 2008 10:07:43 +0200, Reinert Korsnes wrote: >>> >>>> Question: How can I be sure that "Send_Stack" is empty >>>> at the start of the program execution ? >>> >>> Hmm, "sure" in which sense? To make it visible for the reader? To >>> specify in the contract of Stack that it is initially empty? >> >> Yes, yes, to make it visible for the reader. > > I.e. you want to allow initially non-empty stacks? This does not look like > a good idea. Maybe a bit amataur, but down in the code I now have: Node(i).Send_Stack := Message_Stack_p.Empty_Stack; (thanks to your suggestions here) Maybe I better could define a procedure "Empty_Stack(Send_Stack)" ? reinert > > Anyway, if stack is non-limited, you could do something like > > type Stack (<>) is private; > -- The box <> enforces object's initialization > Empty : constant Stack; -- Initial value of an empty stack > ... > private > ... > Empty : constant Stack := null; > > then the user will be forced to do: > > Send_Stack : Stack := Empty; > > However, I see no reason to have stack copyable in the first line. > >>> As for implementation you posted, the stack is empty, because instances >>> of access types are initialized with null (when not explicitly >>> initialized >> >> Yes, but I do not like things depend on the particular implementation :-) > > But this particular implementation fulfills the stack contract, which > reads "stack is initially empty." I see no problem here. > >>>> procedure Pop(S: in out Stack; X: out Item) is >>>> begin >>>> X := S.Value; >>>> S := S.Next; >>>> end; >>> >>> This is a memory leak. If you allocated a stack element on push, you >>> should free it on pop. >> >> How I free it? I may not have a deep enough understanding here :-) > > Per a call to instantiated Ada.Unchecked_Deallocation. > > procedure Pop (S: in out Stack; X: out Item) is > procedure Free is new Ada.Unchecked_Deallocation (Cell, Stack); > Top : Stack := S; > begin > if Top =null then > raise Empty_Stack_Error; > else > X := Top.Value; > S := Top.Next; > Free (Top); > end if; > end Pop; > > BTW, you don't need to keep the stack depth in its items. Do it in the > stack object. Increment it on push and decrement on pop. > >>> 3. Package initialization cannot help you here, because the package >>> declares an abstract data type of which objects can be created long >>> after the package itself was elaborated (initialized). >> >> But I would like to make it clear for all that the stack is >> empty at the start of my program! > > But your program starts even before package elaboration. At that point > there is no stack at all, whether empty or not... > >> (also after that I may >> change the implementation). > > Any implementation shall respect the stack contract, and see above... > >>> How are you going to maintain this? Do you want to prevent messages from >>> being copied? Then you should reconsider the design of messages allowing >>> their queuing without stacks. Alternatively, do you want to copy >>> messages upon queueing (to marshal them)? Then the queue should deal >>> with unconstrained objects: >>> >>> generic >>> type Message (<>) is private; >>> package Queue is >>> ... >> >> I want to "stack away" messages to be processed later. >> Copied, deleted etc. > > That does not define what happens with the message object. Do you stack > *the* message or *a* copy/equivalent/digest of? >