Wednesday, January 7, 2009

Solve the problem : stack overflow

I am a poor guy in computer programming.
Today when I run a recursive loop , my program crash!! It told me: stack overflow!

If I use Vagrind to check memory leak, it seems like no more memory can be added to this stack.
I have thought the memory of my computer is so big that it can not crash like this. But happens as, when I run a program ,the complier will tell computer how much space this program need . If it need more ,then stack crash .
To solve this problem:
1, make the size of stack bigger..
2, change the method to decrease the program space .

I choose 2th method..Then I find the solution:
2.1,change the recursion to iteration
2.2,change the recursion to tail recursion

As I use the tree recursion , one function will call itself many times in its loop. For every call, the stack will save the path to parent function call , and save the local variable .

I find my program maybe difficult to be changed to iteration or tail recursion(it will wrong as I can not return for every function).

So the only choice might be elimination the local variable..Then I use more function and input parameters instead of using local variable..

Finally, for this ,it works...and become faster....It help me to do many works ,like region grow ,label connect component and morphological algorithm..

For more information:

No comments: