Shop OBEX P1 Docs P2 Docs Learn Events
Method starting itself over and over again. A no-no? — Parallax Forums

Method starting itself over and over again. A no-no?

SkogsgurraSkogsgurra Posts: 231
edited 2007-09-03 18:06 in Propeller 1
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.

My questions:
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?· :-)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-09-02 20:17
    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 smile.gif )
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-09-02 20:57
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-09-03 15:22
    Thanks guys!

    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.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-03 16:49
    @Skogsgurra: It not quite clear to me what you are talking about - maybe it's your FORTH root smile.gif
    I think you mean "method call" when you say "instantiation" ??
    It would be helpful, when you give same code.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-09-03 17:14
    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
    Propeller Applications Engineer

    Parallax, Inc.
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-09-03 17:38
    Thanks again.

    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?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-03 18:06
    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
Sign In or Register to comment.