View Full Version : Method starting itself over and over again. A no-no?
09-03-2007, 02:59 AM
Hello! I am somewhat confused now. And I don't even know if I should be confused.
I have this word (excuse my FORTH roots) named PUB MEASURE. At the end I have a "getkeys" and a case to select further actions like PRINT, STORE etc. A few days ago, I was not watching myself and today I noticed that I had added "NEW MEASUREMENT" to the case, and actually was starting MEASURE again. It seems to have worked for quite a while before I found out what bad I had done.
1 Is this a working but no-no thing to do?
2 Why does it work (I think it is an effect of the no-stack mechanism, there is no return stack to overflow)
3 Or - is it a perfectly legal way to mess up a program structure?
4 Or - am I completely mistaken - it doesn't work at all. (But it behaves like it works)
5 If it works. Is it part of the Prop thinking - or did it just happen?· :-)
09-03-2007, 03:17 AM
SPIN routines can be called recursively; they have to as they have to be prepared for concurrent use; they are "re-entrent" in computer science speak. The "default stack" starts after the last VAR element and thus can be quite large.
When you start more (SPIN-) COGs you have to provide an extra stack for each of them; those stacks are genaral small. If they overflow disaster is imminent!
There are no good rules for these stack sizes; I generally recommend "20", and when somthing funny happens, enlarge it to 100, and use a "used stack check routine" (I have forgotten where it is, doing mostly machine code http://forums.parallax.com/images/smilies/smile.gif )
09-03-2007, 03:57 AM
Phil's stack monitor is here: http://forums.parallax.com/showthread.php?p=577317
09-03-2007, 10:22 PM
Good to know that it isn't completely illegal.
But the fact that every instantiation eats memory is discomforting. Would that mean that I can do it only so many times - and then bombs?
I have already changed that code. But still interested to hear what would happen.
09-03-2007, 11:49 PM
@Skogsgurra: It not quite clear to me what you are talking about - maybe it's your FORTH root http://forums.parallax.com/images/smilies/smile.gif
I think you mean "method call" when you say "instantiation" ??
It would be helpful, when you give same code.
09-04-2007, 12:14 AM
All recursive procedures eat stack (assuming they use stacks for function calls), the return address and local variables must be kept until a procedure is returned, and if you infinitely recurse that never happens and hence the stack overflow. This is true of any stack based language, C, FORTH, lisp, PASCAL,...
Paul Baker (mailto:email@example.com)
Propeller Applications Engineer
Parallax, Inc. (http://www.parallax.com)
09-04-2007, 12:38 AM
Yes deSilva. I probably meant "method call", but I didn't want to use that expression since I didn't see it as a call - more as "a use of", which may be the same thing.
Are there any rules as to what to call things in this non-object, but nonetheless somewhat, object oriented language?
09-04-2007, 01:06 AM
I think - though this is quite personal - the terms: method, (sub)routine, procedure - even function - could be used synonymiously.
In SPIN "objects" are in fact "static objects", but it would be more appropriate to call them "module templates". They are more than traditional "modules"as they can have a multiplicity, similar to the instantiation of objects in OOP.
This is important to know, as this instantiation can happen accidentially (with most irritating effects) when using a module name in different objects of the program tree.
Post Edited (deSilva) : 9/3/2007 8:39:43 PM GMT