控制堆栈溢出的技术?
基本上,我的程序将尝试生成所有可能的小写 5 个字母单词的列表。包括所有显然不是真正单词的组合,例如 jshcc 或 mmdzq。
我通过堆积大量的函数调用来做到这一点,这使得这个词起作用。
但这实在是太多了,我收到了堆栈溢出错误。
有人会如何控制它?
Basically, my program will try to generate the list of all possible lowercase 5-letter words. Including all combinations that clearly are not real words like jshcc or mmdzq.
I do that by stacking up a massive amount of calls for a function, which does the word work.
But that's simply too much, and I get a stack overflow error.
How would someone control that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,从递归转换为迭代。通常,这涉及创建一个
Stack
作为“逻辑”堆栈或类似的东西。然而,我本以为生成所有可能的 5 个字母单词列表的方法只会有一个大约 5 深的堆栈 - 每个字母一个。每个堆栈级别将负责一个字母级别 - 因此堆栈的“顶部”将迭代每个可能的最后一个字母;下一个堆栈帧将迭代所有可能的第四个字母,递归调用该方法以迭代所有可能的最后一个字母等。类似这样的东西(C#代码,但希望你能理解它并将其应用到VB):
现在,如果你不这样做没有任何类型的过滤,这将生成 11,881,376 个单词 - 每个单词 24 字节(在 x86 上)约为 285MB - 加上列表等的所有空间。不应该杀死一台足够大的机器,但它内存相当大。您确定您需要所有这些吗?
Basically, convert from recursion to iteration. Typically that involves creating a
Stack<T>
as a "logical" stack, or something similar.However, I'd have expected a method generating a list of all possible 5-letter words to only have a stack about 5 deep - one for each letter. Each stack level would be responsible for one level of letter - so the "top" of the stack would iterate through each possible last letter; the next stack frame down would iterate through every possible fourth letter, calling the method recursively to iterate through all possible last letters etc. Something like this (C# code, but hopefully you can understand it and apply it to VB):
Now if you don't have any sort of filtering, that's going to generate 11,881,376 words - which at 24 bytes each (on x86) is about 285MB - plus all the space for the list etc. That shouldn't kill a suitably big machine, but it is quite a lot of memory. Are you sure you need all of these?
作为一个简单的解决方案,我将使用具有多个循环的迭代方法来生成这些单词:
As a simple solution, I would use an iterative method with multiple loops in order to generate these words: