comp.lang.ada
 help / color / mirror / Atom feed
From: "Grein, Christoph" <christoph.grein@eurocopter.com>
Subject: Re: A tiny little integer stack package from a novice.
Date: Mon, 25 Nov 2002 08:33:27 +0100 (MET)
Date: 2002-11-25T08:33:27+01:00	[thread overview]
Message-ID: <mailman.1038210003.31811.comp.lang.ada@ada.eu.org> (raw)

> with Ada.Unchecked_Deallocation;
> generic
> 
> 	Size : Positive;
> 		
> package int_stack is
> 	
> 	type Int_Stack is limited private;
>	type Stack_Ptr is limited private;

Why are these two types visible? There is nothing one can do with them.
A user or your package can declare variables of those types, but they will never 
be used by your package.

> 	
> 	procedure Push(X : in Integer);
> 	procedure Free_Stack;

Once you've called Free_Stack, you can never again use it. Is this really what 
you want?


> 	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);

Why do you use dynamic allocation? There are other possibilities.


> end int_stack;
> 	
> 
> 
> package body int_stack is 
> 	
> 	Counter : Positive := 1;
> 	
> 	procedure Push(X : in Integer) is
> 	
> 	begin
> 	
> 		The_Stack.all(Counter) := X;
> 		
> 		Counter := Counter + 1;
>

What about overflow here or underflow below?
	
> 	end Push;
> 	
> 	procedure Free_Stack is
> 	
> 	begin
> 	
> 		Free(The_Stack); -- I'm learning to make the commands 
> 						 -- self-explanatory here. Heh.
> 		
> 	end Free_Stack;
> 		
> 	function Pop return Integer is
> 	
> 		X : Integer := 0;
> 		
> 	begin
> 		Counter := Counter - 1; -- The Counter op is placed here because
> 								-- the Counter 
variable will point to 1 element
> 								-- past the end 
of the array on the first
> 								-- call.
> 		X := The_Stack(Counter);
> 		
> 		return X;
> 		
> 	end Pop;
> 	
> end int_stack;

There is mnre to say, but suffice this for the moment. I think when you ponder 
about the above, you'll see further enhancement possibilities.



             reply	other threads:[~2002-11-25  7:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-25  7:33 Grein, Christoph [this message]
  -- strict thread matches above, loose matches on Subject: below --
2002-11-25  7:19 A tiny little integer stack package from a novice Stapler
2002-11-25 17:31 ` Matthew Heaney
2002-11-25 18:54   ` Stapler
2002-11-25 19:21 ` Jeffrey Carter
2002-11-25 21:40   ` tmoran
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox