精华内容
下载资源
问答
  • 为什么会堆栈溢出问题?

    万次阅读 2019-04-15 14:34:19
    在一个算法中,如果递归函数调用过多次数,那么就导致堆栈溢出。 原因就是,操作系统自动给每个进程分配一个最大栈空间2M,如果超过了这个上限,就导致递归函数执行终止,所以就报错。递归就像你一直在往一...

    在一个算法中,如果递归函数调用过多次数,那么就会导致堆栈溢出。

    原因就是,操作系统会自动给每个进程分配一个最大栈空间2M,如果超过了这个上限,就会导致递归函数执行终止,所以就会报错。递归就像你一直在往一个空间里放东西,也就是一直在入栈,调用一次会把内存地址进行一次入栈,直到调用结束,才会将地址出栈。想一想,是不是如果调用次数过多,入栈的内存地址大于2M,就会引起程序报错呢?

    同样的,如果你创建一个数组过大,会引起堆溢出,操作系统给每个进程分配的最大堆空间是4G,如果过大会导致堆溢出

    ※(调用一个方法,在这个方法执行前都会将之前的内存地址(也就是调用点)入栈,等被调用的方法执行完将地址出栈,程序根据这个数据返回调用点)

    解决递归函数堆栈溢出的方法就是尾递归:

    尾递归就是在函数返回return时调用函数本身,而不使用其他表达式。这样执行的时候尾递归函数只会占用一个栈帧,就不会引起栈溢出。

    下面是2个例子:

    def fact(n):
        if n==1:
            return 1
        return n * fact(n - 1)
    def fact_iter(num, product):
        if num == 1:
            return product
        return fact_iter(num - 1, num * product)

    第一个就是递归函数;第二个就是优化后的尾递归。

     

     

    很经典的递归算法例子-----【汉诺塔】:

    汉诺塔是由三根杆子A,B,C组成的。A杆上有N个(N>1)穿孔圆盘,盘的尺寸由下到上依次变小。要求按下列规则将所有圆盘移至C杆:每次只能移动一个圆盘;大盘不能叠在小盘上面。提示:可将圆盘临时置于B杆,也可将从A杆移出的圆盘重新移回A杆,但都必须尊循上述两条规则。问:如何移?最少要移动多少次?汉诺塔是根据一个传说形成的一个问题:

    有三根杆子A,B,C。A杆上有N个(N>1)穿孔圆盘,盘的尺寸由下到上依次变小。要求按下列规则将所有圆盘移至C杆:

    每次只能移动一个圆盘;

    大盘不能叠在小盘上面。
     

    以下是,答案及思路:

    def move(n, a, b, c):
        if n == 1:
            print('move', a, '-->', c)
        else:
            move(n-1, a, c, b)  //先将初始柱A的前n-1个盘子借助目的柱C移动到借用柱B上
            move(1, a, b, c)   //将A柱剩下的一个最大盘子移动到目标C柱上
            move(n-1, b, a, c)  //最后将B柱上的n-1个盘子移动到目标C柱上

    move(4, 'A', 'B', 'C')

    preview

     

    展开全文
  • link编写的代码如何使用尾递归?为什么尾递归还会堆栈溢出
  • 堆栈溢出

    2018-06-01 11:25:12
    单片机编程 时 如果 堆栈溢出,可以将大的临时变量 改全局变量,全局变量不受堆栈的约束。
    单片机编程 时 如果 堆栈溢出,可以将大的临时变量 改为全局变量,全局变量不受堆栈的约束。
    展开全文
  • #include #include #include using namespace std; string f(string alin) { string blin; if (sizeof(alin) == 1) return alin;... blin = alin.substr(0 , 1);... return f(alin.substr(1))+blin;...
  • 内存溢出和堆栈溢出Stack Overflow is an incredible collection of programming and software knowledge. Thousands of students and developers use it daily to lookup answers for questions they can’t solve ...

    内存溢出和堆栈溢出

    Stack Overflow is an incredible collection of programming and software knowledge. Thousands of students and developers use it daily to lookup answers for questions they can’t solve easily themselves. Stack Overflow taught me so much over the years, I have used it since I began to learn to program. It’s built and grown by a community of developers, with new questions and answers being added all the time.

    堆栈溢出是令人难以置信的编程和软件知识的集合。 每天有成千上万的学生和开发人员使用它来查找自己无法轻松解决的问题的答案。 多年来,Stack Overflow教会了我很多东西,自从我开始学习编程以来,我就一直使用它。 它由开发人员社区构建和发展,并始终添加新的问题和答案。

    However, contributing to Stack Overflow, asking questions and answering them, is vastly different from just searching and reading information on the site. Stack Overflow has a very steep learning curve, for a long time, it wasn’t really welcoming toward beginners. If you are interested in using it to grow and further your skills or reputation as a developer, this article is for you. On a site like Stack Overflow, there is no right or wrong, all content will eventually be moderated by the community. Yet, you will want to avoid situations where despite good intentions, you get downvoted to oblivion.

    但是, 促成堆栈溢出,提出问题和回答问题与仅在站点上搜索和读取信息有很大不同。 长期以来,Stack Overflow的学习曲线非常陡峭,对初学者并不那么受欢迎。 如果您有兴趣使用它来发展自己,并提高自己作为开发人员的技能或声誉,那么本文适合您。 在Stack Overflow之类的网站上,没有对与错,所有内容最终都将由社区管理。 但是,您将希望避免尽管有良好的意愿但又被遗忘的情况。

    目的 (The Purpose)

    When people start using Stack Overflow, they often don’t fully understand the purpose of the site. Stack Overflow is not a discussion site or a forum. It’s a knowledge base. That means for whatever questions are asked, any answer should be accurate, specific, and useful to the highest degree. It’s not a place to post partial answers or to ask questions on questions. Answers are upvoted and downvoted to determine the communities opinion on them. There’s one, critically, crucially important detail to understand about upvotes and downvotes: Roughly 80% of all votes on Stack Overflow are downvotes.

    当人们开始使用Stack Overflow时,他们通常并不完全了解该站点的用途。 堆栈溢出不是讨论站点或论坛。 这是一个知识库。 这意味着,无论提出什么问题,任何答案都应准确,具体且在最大程度上有用。 这不是发布部分答案或提出问题的地方。 回答是否正确,可以确定社区对此的看法。 有一个非常关键,非常重要的细节需要了解有关投票和降票的信息:关于Stack Overflow的所有选票中大约有80%是投票。

    In fact, there’s even some users with hundreds of thousands in points that have an up to down vote ratio of over one to one thousand. That means for every one up vote they gave, they also gave over a thousand down votes. This isn’t meant to scare you, it’s to help you understand Stack Overflow does have a high standard of quality for content. Even the best answers also get their share of down votes. Any answer you give should have careful thought and effort put into it.

    实际上,甚至有些用户拥有数十万的点数,其最高投票率超过一千。 这意味着他们每投票一票,他们也会投票一千票。 这并不是要吓you您,而是要帮助您了解Stack Overflow确实具有高标准的内容质量。 即使是最佳答案,也将得到他们的反对。 您给出的任何答案都应该经过认真的思考和努力。

    Being a knowledge oriented site can also imply there’s no room for subjectivity. To an extent that’s true, but it’s more complicated than that. Whether or not something is considered subjective depends on how much information exists on that particular problem, or if there is a widely accepted solution. For example, a question asking “How to add more items to a list in Python?” could only be reasonably answered by suggesting to use the append or insert methods of a Python list. An answer giving a solution by construction an entirely new list, would likely be heavily downvoted. That’s because a list in Python is designed for mutability, answers diverting from that use will simply not be useful or welcomed.

    作为一个面向知识的网站,也可能意味着没有主观性。 在一定程度上是对的,但要复杂得多。 是否认为某件事是主观的,取决于有关该特定问题的信息量或是否存在被广泛接受的解决方案。 例如,一个问题询问“如何在Python中将更多项目添加到列表中?” 只能通过建议使用Python列表的append或insert方法来合理地回答。 通过构造一个全新的清单来给出解决方案的答案可能会被严重否决。 这是因为Python中的列表是为可变性而设计的,从该用法中转移出来的答案将根本无用或不受欢迎。

    Asking “How to ensure a particular module is only imported from a certain directory?” is a question that’s more open to interpretation and does not have a widely accepted methodology. A simple answer to such a question would be to suggest inserting the directory path at the beginning of sys.path . Although that answer works, it’s still not full proof. The Python interpreter actually checks sys.modules before it checks sys.path . There’s also a completely different way to achieve the same desired behavior, to run Python in a virtual environment, avoiding conflicts between module names altogether. The point is, there’s multiple ways to answer such a question, given any answer accurately argues for itself.

    “如何确保特定模块仅从特定目录导入?” 这个问题更易于解释,没有广泛接受的方法论。 一个简单的答案就是建议在sys.path的开头插入目录路径。 尽管该答案有效,但仍不是完整的证据。 Python解释器实际上检查sys.path 之前先检查sys.modules 。 还有一种完全不同的方式来实现相同的期望行为,以便在虚拟环境中运行Python,从而完全避免模块名称之间的冲突。 关键是,给定任何答案都可以正确辩解的情况,有多种方法可以回答这样的问题。

    Next, look at both the best, and worst ways you can use Stack Overflow to grow and improve as a developer.

    接下来,研究使用Stack Overflow作为开发人员来成长和改进的最佳和最差方法。

    最差 (Worst)

    As mentioned previously, there’s no wrong way to use Stack Overflow. Yet, there are many pitfalls and traps you can avoid to prevent being heavily downvoted. These are those patterns of contribution you will want to avoid.

    如前所述,使用堆栈溢出没有错误的方法。 但是,您可以避免很多陷阱和陷阱,以防止被严重否决。 这些就是您希望避免的贡献模式。

    问简单的问题 (Asking simple questions)

    Most simple questions are likely to be closed as duplicates on Stack Overflow. This happens to questions that are usually have a solution found easily in a language’s or package’s documentation. While this won’t subtract points from your total score on the site, it’s just a waste of time since you won’t get your question upvoted. Both questions and answers can be voted on, up votes in either case increase points, while down votes decrease them. Try to search if you question exists before you ask it.

    大多数简单问题很可能会作为Stack Overflow上的重复项关闭。 这通常发生在通常在语言或软件包的文档中很容易找到解决方案的问题上。 尽管这不会从您在网站上的总得分中减去分数,但这只是浪费时间,因为您不会提出自己的问题。 可以对问题和答案都进行投票,无论哪种情况,都可以提高投票数,而减少投票则可以降低分数。 询问之前,先尝试搜索是否存在问题。

    Keep in mind, sometimes questions are mistakenly marked as duplicates. This can be because you weren’t clear enough on the specific answer or knowledge you sought. Good news, Stack Overflow allows you to edit a question and reopen it if this happens, netting you more points if that edit is better received.

    请记住,有时问题被错误地标记为重复。 这可能是因为您对所寻求的具体答案或知识不够清楚。 好消息,Stack Overflow允许您编辑问题并在出现这种情况时将其重新打开,如果更好地接受了此编辑,则可以为您赢得更多的分数。

    提供与问题不符的答案 (Giving answers that don’t match the question)

    This might sound obvious, whatever answer you contribute should be suited for the question being asked. Stack Overflow enforces a rather strict interpretation of this rule, though. Submitting an answer which suggests a solution that either doesn’t fully resolve the problem, or uses a different tool or method than what’s specified, is highly frowned upon, and will result in downvoting. This is probably the absolute worst way to use Stack Overflow.

    这听起来似乎很明显,无论您提供什么答案都应该适合所提出的问题。 但是,堆栈溢出强制对此规则进行了相当严格的解释。 提交的答案表明解决方案不能完全解决问题,或者使用的工具或方法与指定的方法不同,因此对此表示强烈的反对,这将导致投票失败。 这可能是使用堆栈溢出的绝对最糟糕的方法。

    An example can be as follows. There is a question, “How to get the stderr from a process with Popen?”. You answer, “This can be accomplished using subprocess.run and accessing the stderr attribute of the return value”. This answer would be considered inappropriate for the question, because it’s specifying the use of a different builtin object the question is not asking about. Stack Overflow is not a discussion forum, and such answers are not useful or desirable on that particular question.

    一个例子可以如下。 有一个问题, “如何从使用Popen的进程中获取stderr?”。 您将回答: “这可以使用subprocess.run并访问返回值的stderr属性来完成”。 该答案将被认为不适合该问题,因为它指定使用该问题未询问的其他内置对象。 堆栈溢出不是讨论论坛,并且这样的回答对于该特定问题没有用或不希望有。

    用热门标签回答问题 (Answering questions with highly popular tags)

    Questions on Stack Overflow are organized by tags relating to the specific topic of software development they are concerned with. The site holds tens of thousands of tags, encompassing every topic you can think of. The most popular of which are general programming language tags, such as [javascript] or [python] . The most popular tags have hundreds of thousands, if not millions of questions listed under them. While you may see a lot of questions that feel easy to answer, there are some substantial risks.

    关于堆栈溢出的问题按与他们所关注的软件开发特定主题相关的标签来组织。 该网站拥有成千上万个标签,涵盖了您可以想到的每个主题。 其中最受欢迎的是通用编程语言标签,例如[javascript][python] 。 最受欢迎的标签下有成千上万个问题,即使不是数百万个问题。 尽管您可能会觉得很多问题很容易回答,但存在一些重大风险。

    Remember the user I mentioned that gives over a thousand downvotes per one up vote? Users similar to that are likely top contributors and lurking in popular tags. Answers you give to questions under these tags will be under high scrutiny. Additionally, many questions in such tags get answered very quickly, as many people are also looking at them. By the time you write a well thought out answer, some top contributor may have already answered it before you. Or, your answer or code may miss small details involved in answering the question, leading to a higher degree of downvotes. Generic, language syntax type of questions are frequently marked as duplicates, so you may start putting effort toward an answer for nothing.

    还记得我提到过的用户,每一次投票就有一千多票的赞成票吗? 与此类似的用户可能是最主要的贡献者,并且潜藏在流行标签中。 您对这些标签下的问题的回答将受到严格审查。 另外,此类标签中的许多问题很快就会得到回答,因为很多人也在看它们。 当您编写一个经过深思熟虑的答案时,某些杰出贡献者可能已经在您面前回答了。 或者,您的答案或代码可能会错过回答问题所涉及的小细节,从而导致更高的否决率。 通用的语言语法类型的问题经常被标记为重复项,因此您可能会开始全力以赴地寻求答案。

    最好 (Best)

    These strategies and tips are meant to help you achieve the most up votes with the least down votes.

    这些策略和技巧旨在帮助您获得最多的赞成票而最少的反对票。

    用不太受欢迎的标签回答问题 (Answering questions in less popular tags)

    In less popular tags, such as [csv] or [bson] , questions stay unanswered for much longer periods of time. Such tags also have far less super contributors (reputation > 100k) you have to compete with. There’s also less intensive, harsh moderation going on. I am by no means suggesting that moderation is bad or counterproductive. On the contrary, it’s what builds Stack Overflow as a community. However, not all moderation is conducive to increases in quality.

    在不太受欢迎的标签(例如[csv][bson] ,问题会在更长的时间内保持未回答状态。 这样的标签所具有的超级贡献者(声誉> 100k)也要少得多。 另外,强度较低,苛刻的调节正在进行中。 我绝不暗示节制是有害的或适得其反。 相反,这是将Stack Overflow建立为社区的原因。 但是,并非所有节制措施都有助于提高质量。

    I wrote this answer to a question I had asked which to my surprise, no one answered. I explained the solution and all the needed steps for others to implement it if they found themselves in a similar situation. Yet, my answer was still downvoted, due to using a different, yet equivalent keyword argument in the setup function for a Python package. There’s no difference in behavior between the two argument fields, but was still marked down by the community. If I were to write that question again, I wouldn’t include the [python] tag.

    我写了这个答案回答了一个我很惊讶的问题,没有人回答。 如果他人发现自己处于类似情况,我将解释该解决方案以及所有其他实施该解决方案所需的步骤。 但是,由于在Python程序包的设置函数中使用了一个不同但等效的关键字参数,我的回答仍然不理想。 这两个参数字段之间的行为没有差异,但仍被社区标记。 如果我要再次写下该问题,则不会包含[python]标记。

    提出没有明显解决方案的问题 (Asking questions with no obvious solution)

    The best questions to ask, and the ones that will get the most upvotes, are the ones with no obvious solution. This doesn’t mean super complex questions almost no one can answer or solve. Questions that concern a common situation, but that have no easily identifiable or straight forward solution, are the ones you need to be asking.

    最好的问题是那些没有明显解决方案的问题,它们将获得最多的投票。 这并不意味着几乎没有人能够回答或解决的超级复杂问题。 您需要提出的问题涉及一个常见情况,但没有容易识别或直接解决的问题。

    I have question that I asked five years ago that has since gotten over 50,000 views. The question asks how to match a particular string against many different possible strings using regular expressions. Generally, checking if a string is present in a group of strings can be accomplished with a hash set. But in the case of regex, there is no widely used method. One option, which turned out as the best answer, is using a list of words joined with the| “or” operator in regex. Another option, the second answer listed, is using named lists from a common third party library for regular expressions. Both answers work fine, but the former doesn’t depend on any other packages, it uses native Python.

    我有一个问题 ,我五年前问过,自那以后获得了50,000多次浏览。 该问题询问如何使用正则表达式将特定字符串与许多不同的可能字符串进行匹配。 通常,可以使用哈希集完成检查字符串组中是否存在字符串。 但是对于正则表达式,没有广泛使用的方法。 事实证明,这是最好的选择,其中一个选项是使用带有|的单词列表。 正则表达式中的“或”运算符。 另一个选择是列出的第二个答案,它使用来自公共第三方库的命名列表进行正则表达式。 两个答案都可以正常工作,但是前者不依赖于其他任何软件包,它使用本机Python。

    The lesson here is asking questions with no direct or straight forward answers are more likely to be upvoted, and more likely to generate answers.

    此处的课程是询问没有直接或直接答案的问题,更可能被否决,并且更有可能产生答案。

    结论 (Conclusion)

    Overall, Stack Overflow is an immense body of knowledge that’s built by a vast community of developers. Using it is easy, contributing to it isn’t that easy. However, if you put careful thought into your answers, stick to less popular tags, and try to ask more complex questions, you will gain a lot more out the experience.

    总体而言,堆栈溢出是由大量开发人员构建的大量知识。 使用它很容易,贡献它并不是那么容易。 但是,如果您对答案进行仔细的思考,坚持使用不太受欢迎的标签,并尝试提出更复杂的问题,您将获得更多的体验。

    翻译自: https://medium.com/swlh/the-best-and-worst-ways-to-use-stack-overflow-711a077f2892

    内存溢出和堆栈溢出

    展开全文
  • 堆栈溢出

    2020-01-11 00:59:53
    当存储的数据达到某一限制时就造成堆栈溢出。注意:栈当中的数据一旦运行完自动清除掉,所有栈不会溢出;所有说的堆栈溢出是指堆溢出,而堆中存储的地址是有关栈的,因此叫堆栈溢出堆栈溢出的原因是:内存...

    堆栈溢出?

    当存储的数据达到某一限制时就会造成堆栈溢出。注意:栈当中的数据一旦运行完会自动清除掉,所有栈不会溢出;所有说的堆栈溢出是指堆溢出,而堆中存储的地址是有关栈的,因此叫堆栈溢出。

    堆栈溢出的原因是:内存泄漏,即当不断向堆中存储数据,而不进行清理,这就是内存泄漏。内存泄漏的结果就是堆栈溢出。

    解决内存泄漏/堆栈溢出的是垃圾回收机制。在其他编程语言中,这种清理分为两种,一种是自动清理(像布尔值),一种是手动清理(gc)。在JavaScript中只有自动清理(即要清理把浏览器关了就行)。

    垃圾回收机制就是将引用堆中地址的对象设置为null,并且将所有引用该地址的对象都设置为null,注意不会立即清除,垃圾回收车会根据内存的情况在适当的时候清除堆中的孤儿对象。

    展开全文
  • //执行1000次左右发生堆栈溢出的错误, //setTimeout(foo, 0); //永远不会堆栈溢出 } foo() 这是什么原理? 请问假如我自己把callback函数也定义成setTimeout这样的,应该怎么写?
  • link中如何避免堆栈溢出,用try catch可以阻止堆栈溢出么?
  • JavaScript之堆栈溢出

    2019-09-11 10:04:14
    一、什么堆栈溢出? 每次执行JavaScript代码时,都会分配一定尺寸的栈空间(Windows系统中为1M),每次方法调用时都会在栈里储存一定信息(如参数、局部变量、返回值等等),这些信息再少也占用一定空间,如果...
  • 堆栈溢出及其原因

    千次阅读 2016-10-02 00:04:46
    在一个程序中,分配给堆栈的内存是有一定限度的。由于C语言系列没有内置检查机制来确保...堆栈溢出有意想不到的结果发生,甚至程序运行崩溃。 一般来说,造成堆栈溢出的原因主要有以下几个: 1、开了数据非常
  • VS堆栈溢出异常

    千次阅读 2016-11-01 15:39:48
    vs堆栈溢出
  • Delphi测试缓冲区溢出 堆栈溢出,本程序用于堆栈溢出测试,超出6个字符的话则溢出。作者:曾繁威 于重庆学习作。输入“aaaaaaaaaaL$E”,可以溢出执行“溢出执行”按钮的代码。
  • 内核堆栈溢出

    千次阅读 2016-11-30 23:15:12
    Linux内核的堆栈一般是8K,所以在写内核程序的时候一定要小心,防止函数递归的出现,递归可能导致内核堆栈溢出。 1. 查看Linux内核线程堆栈,源代码里一般定义 #define THREAD_SIZE (2*PAGE_SIZE) 2. 如果...
  • 1、确实是你的堆栈溢出了,在project->option->c/c++ Complier->system里面改大!! 2、假相,是你的仿真器或者目标板出问题。仿真器在读取寄存器的值的时候出现问题。读过来的数据错误!!一般来说是第2种情况...
  • 我们在使用电脑过程中,常常碰到各种各样的问题,例如最近有用户表示操作电脑过程遇到了浏览器网页弹出“来自网页的消失”对话框,并且系统出现提示“堆栈溢出”,那么堆栈溢出什么意思呢?又应该怎么解决呢?下面...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 78,748
精华内容 31,499
关键字:

为什么会堆栈溢出