Recursive function call limit?

Blitz3D Forums/Blitz3D Programming/Recursive function call limit?

Gillissie(Posted 2003) [#1]
I have a function that recursively calls itself. I am finding that if it calls itself too many times, the program simply quits without any kind of error message or warning. Is there a hard-coded stack limit in Blitz for recursively calling a function? Or is it limited by the PC's memory?


Richard Betson(Posted 2003) [#2]
Blitz does not handle recursion. It may be a stack limit, but recursion is not possible. :(

L8r,


Zethrax(Posted 2003) [#3]
'Blitz does not handle recursion. It may be a stack limit, but recursion is not possible. :('

As far as I'm aware Blitz handles function recursion OK. I remember reading a post a while back which mentioned a limit to the memory used for the stack, however. I think the memory size mentioned was about four kilobytes but don't quote me on that. I think this was in relation to one of the very early versions of blitz.


morduun(Posted 2003) [#4]
Blitz recurses just fine in my experience, tho I don't know explicitly what the stack limits are off the top of my head.


Floyd(Posted 2003) [#5]
Blitz exhibits unusual behavior when it runs out of stack space.

There is a limit to stack size.
If your program exceeds this then you get the error...

Stack overflow!

... when running with Debug Off.

Perversely, with Debug On there is no error message. The program silently terminates.


Richard Betson(Posted 2003) [#6]
I really should clarify my earlier post. In my opinion recursion in Blitz is possible but I do not recommend it. It can be very unpredictable and is limited by the STACK. An earlier discussion of the subject rendered that recursion in Blitz should be avoided.

I have written thousands of lines of code in Blitz and have easily avoided recursive function calling. :)

L8r,


Michael Reitzenstein(Posted 2003) [#7]
I have written thousands of lines of code in Blitz and have easily avoided recursive function calling. :)

It depends what you are writing, though. Some things absolutely require recursion techniques, whether you use recursive functions or not.


Richard Betson(Posted 2003) [#8]
@Michael

I respectfully disagree. ;)

L8r,


Beaker(Posted 2003) [#9]
Rich - the code archives disagree with you. Most functions that handle entity heirarchies use recursion succesfully. I'm sure there are numerous other examples.


Michael Reitzenstein(Posted 2003) [#10]
I respectfully disagree. ;)

You can, except you would be wrong.


Richard Betson(Posted 2003) [#11]
lol...

I am inflexible on my opinion. :)

L8r,


Rob(Posted 2003) [#12]
But it's a wrong, inflexible opinion :)


Difference(Posted 2003) [#13]
This topic seems to be calling it self. I wonder when an overflow error wil happen. ;)

OT: I use recursion all the time without problems. Mainly for entity heirarchies.


Michael Reitzenstein(Posted 2003) [#14]
OT: I use recursion all the time without problems. Mainly for entity heirarchies.

"OT"? You mean, when a topic flies of into a tangent, talking about the originally intended subject is "OT"? :)


(tu) sinu(Posted 2003) [#15]
when i've tried recursion blitz simply exits without no error.


Koriolis(Posted 2003) [#16]
I have never had any problem with recursion in blitz, and am not sure to see what's new here. In languages such as C, the stack in preallcoated and cannot grow, so if you exceed it you have a stack overflow.
The only difference is that in C compilers you can set the stack size at compile time, where blitz doesn't allow this.
Also I had not noticed that no error message is generated in debug mode, which is clearly not a good thing as you can spend time trying to figure what happened (but the fact that there is a stack overflow is in intself normal).

But it's a wrong, inflexible opinion :)
agreed. In fact it's true that you can always replace function recursion with data recursion (the most direct solution being to manage a stack manually), but it's often less elegant and thus harder to maintain and debug.
In addition Richar Betson, you said you wanted to "clarify" your first post, but you have just said something different. Your initial statement ("recursion is not possible") couldn't have been clearer, and was just wrong.


Neochrome(Posted 2003) [#17]
is it neccery to have something that recursivly calls a function? (i try to avoid them) Blitz can do just about anything you ask it (in 3D) with out much problems


yinch(Posted 2003) [#18]
I have used recursion very effectively in Blitz3D and have never had a problem.

Recursion is known to be an extremely powerful algorithmic technique and is found in every major programming language.

When using recursion, the coder must bear in mind the impact of local variables within the function. The stack is used as the allocation space for these functions' local variables.

So if you have a function with a lot of local variable space, you will run out of stack very very fast.

yinch'03


Steve Hill(Posted 2003) [#19]

In languages such as C, the stack in preallcoated and cannot grow, so if you exceed it you have a stack overflow.



I agree with most of what Koriolis says here, but the
stack issue is not a language issue per se. Rather it is
a compiler/OS property. Some operating systems (e.g. some
flavours of Unix) allow the stack to be grown on demand,
so never run out of stack as such .. programs just use more and more virtual memory.

However, so far as I am aware on Windows you have to say
how much stack you want up-front and then wanton recursion
leads to a stack overflow as Koriolis says.

If recursion is causing headaches, one approach might be
to use Blitz types to model a data stack. This way your
stack is part of the heap and only limited by VM. I used
this technique to manage include files in a preprocessor.

Steve Hill.


Floyd(Posted 2003) [#20]
But in practice it is extremely unlikely that you need more stack space than Blitz provides.

If you are overflowing the stack then the solution is to fix your algorithm.


Koriolis(Posted 2003) [#21]
I agree with most of what Koriolis says here, but the
stack issue is not a language issue per se. Rather it is
a compiler/OS property
True, I made a shortcut. But fact is you won't find a single C compiler that doesn't allow to set the stack size, while for blitz, well theer is only a single implementation of the language and it doesn't allow to set the stack size

[quote]if recursion is causing headaches, one approach might be to use Blitz types to model a data stack[quote]Yep, was what I was talking about when I said you can always convert function recursion into data recursion.


mag.(Posted 2003) [#22]
I love recursion. So far never have any problem with Blitz.

A lot of people say we can avoid recursion.
I wonder how actually to avoid it?

Say for example, calculating user input expression:
a$="3+((12-1)+(23/4))"
We don't know how long that a$ and how many () will be use.
Any sample on how to do that without recursion?


MSW(Posted 2003) [#23]
How fast do you need this recursion operation to run?

If speed isn't the highest priority, you could easily implament a "virtual" stack useing a bank or string...something like this:

Global Vstack$
Vstack$="" ;clear the virtual stack

Vstack$ = Vstack + BIN(first variable) + BIN(second variable) ;etc

loop

result = recursionfunction()

until LEN(vstack) = 0

then use the result value however :P



recursionfucntion ()

myvariables$ = MID(Vstack, LEN(vstack)-(4 * number of variables passed, number of varables passed)

Vstack$ = MID(vstack,1,LEN(vstack) - (4 * number of variables passed))

fist variable$ = MID(myvariables$,1,4)
second varable$ = MID(myvariables,5,4) ;etc..

then convert the 4 character strings into numeral values (I wish Blitz had some functions for this like good old Qbasic had :P) but getting the ASCII values and left shifting works fine...

Do your stuff with the variables...and if you need to call the function again then just add the variables to pass onto the the Vstack...else end the function and the loop around the recursion will keep calling the function until the Vstack string is empty...this is going to be slower then a real stack basied recursion function, but you have a much larger virtual stack space to use...


_Skully(Posted 2003) [#24]
Recursion works just fine.. I use it all the time.. stack space limits are there and it would be nice if it was adjustable in size

I just wish there were static variables.

Skully