字典stackoverflow方法 [英] Dictionary stackoverflow method

查看:99
本文介绍了字典stackoverflow方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果下面的代码是通过16,000条记录的文件,这是否会导致无限循环或堆栈溢出,因为为了文本替换的目的被多次调用?它使用这种方法,因为字典中有重复的单词。我用一本小字典做了一些实验,但它完全可以100%完成。我还用2000条记录的名字字典对它进行了测试,它运行得很好。但是当我尝试我的16,000条记录的字典时,它会取代某些点,然后获得一个stackoverflow。它看起来像这样:

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




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. "

推荐答案

我不确定你是否还在看这个问题,OP,但这就是堆栈溢出错误,以及为什么你提供的代码没有显示问题。



(专业人士:我知道这在很大程度上是不准确的,但我只是想得到基本概念)

所以有两个内存套留给.Net应用程序。堆栈和堆。堆是无序的。你永远不必担心堆,除非你在程序周围移动非常大的对象。



堆栈是在添加内容时订购的。

[比喻时间]

把它想象成桌面上的一叠文件。它是进行中的堆。在这个类比中,你从一个待办事项清单开始。



第一项是写一个目录,这样你就得到一张新的空白表并将它放在待办事项列表的顶部。你不能回到tofo列表,直到任务是C完成。



你在纸上写下目录。



现在你需要添加第1页的标题,但是没有第1页所以你必须得到另一张纸。这是论文堆栈的顶部。



最后你完成了第1页,然后从堆栈的顶部删除它,你可以看到再次显示目录页面。

您乐意写下Page 1:preface等等。



当待办事项上的最后一项完成时,它也会从堆栈中移除。如果是空堆,您可以关闭办公室并回家。



这个堆栈吧非常大。你的堆栈上可能有数百个页面,但仍然可以编写下一个项目。请记住,每个项目在完成后都从堆栈中删除,因此堆栈通常保持相当小。





好​​的,现在 - 坚持这个类比让我们说有人犯了一个错误。目录实际上是第1页



你到达目录并需要写第1页:目录,但是还没有第1页那么你得到一张新纸并写下目录。

你需要写第1页:目录,但还没有第1页,所以你得到一张新纸并写下目录。

你需要写第1页:目录,但还没有第1页,所以你得到一张新纸并写下目录。

你需要写第1页:目录,但还没有第1页,所以你得到一张新纸并写下目录。

它永远不会结束



最终你会得到一堆如此之大的纸,甚至无法再添加另一张纸。恭喜,你有一个堆栈溢出错误!

[类比结束]





所以,你的16k记录不能导致错误。每次完成后,它将从堆栈中删除。这就像2-16k页。它们只是我类比中的第三张。



寻找递归。如果你有一个自己调用的方法,或者你可以找到一个最终在无限循环中相互调用的方法循环,​​那么这就是你的堆栈溢出。那是你的纸塔倒塌了。



我希望有所帮助。



有关堆的更多详情和堆栈,那里有很多资源。入门的好地方是本文:六个重要的。 NET概念:堆栈,堆,值类型,引用类型,装箱和拆箱 [ ^ ]



如果您有任何疑问,请告诉我^ _ ^

Andy
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


这篇关于字典stackoverflow方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆