comp.lang.ada
 help / color / mirror / Atom feed
From: Nick Roberts <nick.roberts@acm.org>
Subject: Re: Objects and the Stack?
Date: Fri, 17 Dec 2004 11:46:11 +0000
Date: 2004-12-17T11:46:11+00:00	[thread overview]
Message-ID: <32fv82F3l9al7U1@individual.net> (raw)
In-Reply-To: <pan.2004.12.17.03.54.12.727310@nowhere.net>

Freejack wrote:
> On Fri, 17 Dec 2004 00:28:23 +0000, Jeffrey Carter wrote:
> 
>  
> 
>>The fact that pointers are generally not needed in Ada except when 
>>creating things such as dynamic data structures has nothing to do with 
>>tagged types. If you create objects of a classwide type, they are put on 
>>the stack if you declare them there, and in a storage pool if you 
>>allocate them there.
>>
> 
> Something like ...
> 	declare
> 		Stack : Obstack'Class := Item'(SomeIndex);
> 	begin
> 		IsPushed := Push(Item);
> 		NewItem := Pop;
> 	end;
> Is what I have in mind. 
> 
> However what I'd like to be able to do( and what I'm working on. ) is
> something like this...
> 
> 	package body obstacks is
> 		type obstack is abstract tagged null record;
> 		
> 		Stack : Obstack'Class; -- Problematic, needs some kind
> 					  of discriminant.
> 
> 		function pkgbodyPush(X : Item) return obstack'class is
> 			type NewStack is new Obstack with
> 				record
> 				Prev : Obstack'Class := Stack;
> 				NewItem : Item := X;
> 				end record;
> 			PushedStack : NewStack; --Again, problematic
> 		begin
> 			return PushedStack;
> 		
> 		end pkgbodyPush;
> 
> 	And then, every time the internal pkgbodyPush is called we get..
> 
> 		Stack := pkgbodyPush(X);
> 
> See where I'm going with this? The above code is riddled with problems
> that I gotta figure out. But that's the gist of it. The "Stack" variable
> should remain on the "stack" as long as the package body is in scope.
> That's my assumption at least. I'll know soon enough if I'm wrong. Heh.
> 	
> 
>>I'm not sure what you mean by "extended" here, whether you're referring
>>to type extension or a value that becomes larger in 'Size, but this
>>doesn't work the way I think you think it will.
> 
> 
> Probably not. But then again, that's what experimentation is for. ;->
> That's one of the ways I learn new programming methods. By taking a
> concept to the most absurd extreme I can imagine.

I suppose so, but I think I should tell you now that what you trying 
(above) cannot work. The assumption in Ada is that the space reserved 
within a stack frame for an object cannot be resized (even though the space 
used /within/ the space reserved for an object can). So the compiler must 
be able to allocate the maximum size of an object on the stack. Since what 
you are attempting could cause Stack to grow to any size without limit, the 
compiler cannot know what maximum to allocate. It won't work, and so you 
must use the heap (or more correctly, a 'storage pool').

Ada has no equivalent of alloca() in C. It might be interesting to 
experiment with a compiler extension to provide this facility. Certain 
algorithms might benefit from it.

    declare
       Name: String := "";
       pragma Extensible_Object(Name);
    begin
       ...
       Name := Name & ':'; -- changes its size
       ...

It's an idea.

-- 
Nick Roberts



  parent reply	other threads:[~2004-12-17 11:46 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-16 22:27 Objects and the Stack? Freejack
2004-12-16 23:13 ` Nick Roberts
2004-12-20 14:50   ` Marc A. Criley
2004-12-20 16:22     ` Marius Amado Alves
2004-12-20 18:31       ` Dmitry A. Kazakov
2004-12-17  0:28 ` Jeffrey Carter
2004-12-17  8:45   ` Freejack
2004-12-17 11:11     ` Martin Dowie
2004-12-17 11:46     ` Nick Roberts [this message]
2004-12-17 19:52       ` Freejack
2004-12-18  4:02         ` Nick Roberts
2004-12-27  4:34         ` Dave Thompson
2004-12-18 19:41       ` Warren W. Gay VE3WWG
2004-12-18 20:50         ` Freejack
2004-12-18 21:15           ` Dmitry A. Kazakov
2004-12-27  4:34         ` Dave Thompson
2004-12-18  0:12     ` Jeffrey Carter
2004-12-18  0:43       ` Jeffrey Carter
replies disabled

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