Jump to content

Help with infinite loop

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
thanasis2028

thanasis2028

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
I have made a function named evaluate that evaluates mathematical expressions and the user can also add their own variables. However, in the code that replaces a variable with its value the evaluate function is recalled, and this, in some cases (for example if the variable is x=x+2 or something like that), causes an infinite loop and throws a StackOverflowException. I tried to catch that exception using Try...Catch. But the exception is not caught. Why is that? Is there any way to prevent the infinite loop?
Here is my code:
    Private Function identifier(ByVal token As String) As Double
        'The function that replaces the variable with its value
        Try
            For i As Byte = 1 To var_name.Count
                'Might be a little confusing if you don't know the whole code 
                'the only thing that needs attention is that if a condition is met 
                'evaluate() is recalled which then recalls identifier() and an infinite loop might occur
                If token.ToLower = var_name(i).ToString Then Return evaluate(var_value(i))
            Next
        Catch ex As StackOverflowException
            'The exception is not actually caught
        End Try
    End Function

My software company:Porosis Software
Games: Terrantula
Apps: Mathanasis, ColorGrabber (New!)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Where is the code that is actually causing the infinite loop? Is this recursive function calls?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
Post the evaluate function code.

#4
thanasis2028

thanasis2028

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
That is the line that causes the infinite loop:
If token.ToLower = var_name(i).ToString Then Return evaluate(var_value(i))
It calls evaluate which calls back identifier and if the variable to be replaced is x=x this will happen forever. It finds x, replaces it with x, and then again the same.
And I can't post the whole evaluate, because it's huge and it won't help either. However, if you want to see it, go to my website and download the source of Mathanasis. (See my sig)
My software company:Porosis Software
Games: Terrantula
Apps: Mathanasis, ColorGrabber (New!)

#5
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
As you are saying it's recursive problem and you may be lacking the right base case. I'll look into that problem when I get back from university.

#6
thanasis2028

thanasis2028

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Ok thanks, but if you look into my source, I warn that when I started making this program I didn't know VB well, and I did lot's of stupid things that make my source to hard to be read. There are some times that evem I can't figure out what the purpose of some pieces of code was. :crying: I could fix those things right now but I am to lazy. It would need to code the whole program from the beginning.
My software company:Porosis Software
Games: Terrantula
Apps: Mathanasis, ColorGrabber (New!)