Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
If the code below were to through a file of 16,000 records, would this cause an infinite loop or a stackoverflow because of being called to many times for Text Replacement purposes? It using this method because, there are duplicate words in the dictionary. I have done some experiments with a small dictionary, but it works 100% percent perfectly. I have also tested it with a names dictionary of 2000 records, and it works perfectly. But when I try my dictionary of 16,000 records, it replaces to some point then gets a stackoverflow. It look like this:
Field1 Field2
mainly|chiefly
mainly|fundamentally
mainly|typically
primarily|mainly
primarily|particularly
particularly|specifically
specifically|typically

VB
Using reader As New StreamReader("C:\Users\Acer\Desktop\wordlist2.txt")
    Do Until reader.EndOfStream
        Dim parts = reader.ReadLine().Split("|"c)

        If replacements.ContainsKey(parts(0)) Then
            replacements(parts(0)).Add(parts(1))
        Else
            Dim newWordList As New List(Of String)
            newWordList.Add(parts(1))
            replacements.Add(parts(0), newWordList)
        End If
    Loop
End Using

RichTextBox1.Text = "You provided a bad advice, irregardless of your intention. You provided a bad advice, irregardless of your intention. "
Posted
Updated 25-May-16 22:24pm
v4
Comments
Richard Deeming 27-Jan-16 9:17am    
The code you've posted will not produce a StackOverflowException, and cannot have an infinite loop. If you're getting a StackOverflowException, then it's coming from some other part of your code.
[no name] 6-May-16 4:58am    
I have added a code below! Could it occur there?
Andy Lanng 27-Jan-16 9:48am    
ha ha: "irregardless".
It's always fun hiding Bushisms in your code but you should correct the user facing text. The word is out of use and should be replaced with the standard "regardless"
also, "advice" is an uncountable noun (like 'water') so "a bad advice" should just be "bad advice", unless you are using the term "advice" to refer to an actual item or thing ('a piece of advice', or 'I have a class called "advice" and i need to instantiate 3 advices')

Dontcha just love English ;)
Sergey Alexandrovich Kryukov 27-Jan-16 11:15am    
5! :-)
—SA
Richard MacCutchan 27-Jan-16 11:21am    
irregardless is commonly heard in USA, and not just by Bush. One of the many invented words that used to irritate me when I visited company HQ, back in the day.

1 solution

I'm not sure if you're still watching the question, OP, but here's what a Stack Overflow error is, and why the code you presented doesn't show the issue.

(pros: I know this is largely inaccurate, but I'm just trying to get the basic concept out)
So there are two memory "sets" set aside for .Net apps. The Stack and the Heap. The heap is "un-ordered". You never have to worry about the heap unless you're moving VERY large objects around a program.

The stack is ordered by when something is added to it.
[Analogy time]
Think of it as a stack of papers on a desk. Its the 'in progress' pile. In this analogy, you start with a todo list.

The first item is to write a table of contents so you get a new blank sheet and put it on top of the todo list. You can't get back to the tofo list until the task is complete.

Your write the words "Table of Contents" on the paper.

Now you need to add Page 1's title, but there is no page 1 so you have to get another piece of paper. This goes to the top of the "stack" of papers".

Finally you finish page 1 and this is then removed from the top of the stack and you can see the "table of contents" page again.
You happily write down "Page 1: preface" and so on and so on.

When the last item on the todo is complete, it is also removed from the stack. With an empty stack you can shut the office and go home.

This stack it pretty large. You could probably have hundreds of pages on your stack and still be able to write the next item. Remember that each item is removed from the stack when it's completed so the stack usually stays pretty small anyway.


Ok, now - sticking with this analogy let's say that someone made a mistake. The table of contents is actually on Page 1

You get to the table of contents and need to write "Page 1: Table of Contents", but there is no page 1 yet so you get a new piece of paper and write "Table of Contents".
You need to write "Page 1: Table of Contents", but there is no page 1 yet so you get a new piece of paper and write "Table of Contents".
You need to write "Page 1: Table of Contents", but there is no page 1 yet so you get a new piece of paper and write "Table of Contents".
You need to write "Page 1: Table of Contents", but there is no page 1 yet so you get a new piece of paper and write "Table of Contents".
It NEVER ends

eventually you going to get a pile of paper so large that you cannot even reach to add another sheet. Congrats, you have a stack overflow error!
[End of analogy]


So, your 16k records can't cause the error. Each time one is complete it is removed from the stack. this would be like the pages 2-16k. They are only ever the third sheet in my analogy.

Look for recursion. If you have a method that calls itself, or if you can find a loop of methods that end up calling each other in an endless loop then that's your stack overflow. That's your tower of paper toppling over.

I hope that helps.

For more details on heap and stack, there are many resources out there. A good place to get started is with this article: Six important .NET concepts: Stack, heap, value types, reference types, boxing, and unboxing[^]

Let me know if you have an questions ^_^
Andy
 
Share this answer
 
Comments
[no name] 6-May-16 5:00am    
I have added a code below! Could it occur there right next to here:

Else
nextCheckIndex += 1
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900