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,LOTS_OF_MONEY autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d3b3a6fb3be6d395 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-11-25 09:31:57 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: mheaney@on2.com (Matthew Heaney) Newsgroups: comp.lang.ada Subject: Re: A tiny little integer stack package from a novice. Date: 25 Nov 2002 09:31:57 -0800 Organization: http://groups.google.com/ Message-ID: <1ec946d1.0211250931.341c1a32@posting.google.com> References: NNTP-Posting-Host: 66.162.65.162 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1038245517 28345 127.0.0.1 (25 Nov 2002 17:31:57 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 25 Nov 2002 17:31:57 GMT Xref: archiver1.google.com comp.lang.ada:31213 Date: 2002-11-25T17:31:57+00:00 List-Id: Stapler wrote in message news:... > generic > > Size : Positive; > > package int_stack is > > type Int_Stack is limited private; > type Stack_Ptr is limited private; > > procedure Push(X : in Integer); > procedure Free_Stack; > > function Pop return Integer; > > private > > > type Int_Stack is array(1..Size) of Integer; > type Stack_Ptr is access all Int_Stack; > The_Stack : Stack_Ptr := new Int_Stack; -- Provides access for De-allocation of entire stack > -- Is also the sole allocation point for this package. > procedure Free is new Ada.Unchecked_Deallocation(Int_Stack, Stack_Ptr); > > end int_stack; Why is this a generic package? Why isn't this an abstract data type? If the stack has a fixed, maximum size, it's easier to simply declare it as a discriminated record, which avoids all the headaches associated with memory allocation. For example: package Integer_Stacks is pragma Pure (Integer_Stacks); type Stack_Type (Size : Natural) is limited private; ... private type Integer_Array is array (Positive range <>) of Integer; type Stack_Type (Size : Natural) is limited record Elements : Integer_Array (1 .. Size); Top : Natural := 0; end record; end Integer_Stacks;