Good explanation for Thread.StackSize

What is the ‘stack’ and in thread?

Thanks in advance.

The stack can be thought of as the list of methods that have yet to return to their caller.

For example, suppose you call this method from your Thread’s Run event:

Function RecursiveExample(Number As Integer) As Integer
     Return RecursiveExample(Number + 1)
End Function

Each time RecursiveExample is called, it gets added to the list of pending method calls (AKA the stack). If the stack runs out of room, as will quickly happen if you test the above code, an exception will be raised. If you know your thread is going to be long method-call chains, you will probably need to increase the size of the stack for the thread.

In Xojo, you can view the stack in the debugger:

If you’re not getting StackOverFlowExceptions in your app, you probably don’t need to change the default stack size.

[quote=38425:@Andrew Lambert]The stack can be thought of as the list of methods that have yet to return to their caller.

For example, suppose you call this method from your Thread’s Run event:

Function RecursiveExample(Number As Integer) As Integer
     Return RecursiveExample(Number + 1)
End Function

Each time RecursiveExample is called, it gets added to the list of pending method calls (AKA the stack). If the stack runs out of room, as will quickly happen if you test the above code, an exception will be raised. If you know your thread is going to be long method-call chains, you will probably need to increase the size of the stack for the thread.

In Xojo, you can view the stack in the debugger:

If you’re not getting StackOverFlowExceptions in your app, you probably don’t need to change the default stack size.[/quote]
Thanks but wouldn’t it be better to use for loops rather than stacks? Why have method call its self when you can just use a for loop. Am I missing something? Thankyou very much for your time.

That, I believe, was simply an example to show how you can overflow the stack. There are plenty of times in your program where you call a method and inside that method another method is called. At that point in time you have two methods on the stack. Just thing of all of the things being called via the interface. Lots of stuff going on the stack behind the scenes as those events are triggered etc.

Some algorithms just naturally lend themselves to being recursive.
Thats not to say there’s ISN’T one that uses a for / do / while loop - it just may not be as obvious how to do that.

Technically, you can convert any Tail-Recursive function into a plain old iterative loop. In fact, Wikipedia claims that some languages do this for you automatically. I’m pretty sure Xojo is not one of those languages :slight_smile: http://en.wikipedia.org/wiki/Tail_recursion