是什么导致堆栈溢出? [英] What is causing a stack overflow?

查看:150
本文介绍了是什么导致堆栈溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能会认为这是一个巧合,我的问题的主题与论坛的名称相似,但实际上我是通过谷歌搜索堆栈溢出"来到达这里的.

You may think that this is a coincidence that the topic of my question is similar to the name of the forum but I actually got here by googling the term "stack overflow".

我使用OPNET网络模拟器,在其中使用C进行编程.我认为大型数组存在问题.看来我遇到了某种内存分配限制.这可能与OPNET,Windows,我的笔记本电脑内存或最有可能的C语言有关.当我尝试使用嵌套数组且元素总数达到几千个整数时,就会引起问题.我想我超出了整体内存分配限制,我想知道是否有办法提高此上限. 这是确切的问题描述:

I use the OPNET network simulator in which I program using C. I think I am having a problem with big array sizes. It seems that I am hitting some sort of memory allocation limitation. It may have to do with OPNET, Windows, my laptop memory or most likely C language. The problem is caused when I try to use nested arrays with a total number of elements coming to several thousand integers. I think I am exceeding an overall memory allocation limit and I am wondering if there is a way to increase this cap. Here's the exact problem description:

我基本上有一个路由表.我们将其称为routing_tbl [n],这意味着我支持30个节点(路由器).现在,对于此表中的每个节点,我保留信息.称为path [p]的数组中包含大约(数百)条可用路径.同样,对于此数组中的每个路径,我将属于它的节点列表保留在名为hops [h]的数组中.因此,我至少使用了nph个整数,但此表也包含其他信息.在同一函数中,我还使用了另一个嵌套数组,该数组也消耗了将近40,000个整数. 一旦运行模拟,它就不再抱怨堆栈溢出.当我减小路由表的总大小时,它可以工作. 您认为导致问题的原因是什么,如何解决? 非常感激 阿里

I basically have a routing table. Let's call it routing_tbl[n], meaning I am supporting 30 nodes (routers). Now, for each node in this table, I keep info. about many (hundreds) available paths, in an array called paths[p]. Again, for each path in this array, I keep the list of nodes that belong to it in an array called hops[h]. So, I am using at least nph integers worth of memory but this table contains other information as well. In the same function, I am also using another nested array that consumes almost 40,000 integers as well. As soon as I run my simulation, it quits complaining about stack overflow. It works when I reduce the total size of the routing table. What do you think causes the problem and how can it be solved? Much appreciated Ali

推荐答案

如果发布一些代码可能会有所帮助.编辑问题以包括问题功能和错误.

It may help if you post some code. Edit the question to include the problem function and the error.

同时,这是一个非常通用的答案:

Meanwhile, here's a very generic answer:

导致堆栈溢出的两个主要原因是1)递归函数或2)分配了大量局部变量.

The two principal causes of a stack overflow are 1) a recursive function, or 2) the allocation of a large number of local variables.

递归

如果您的函数调用自己,如下所示:

if your function calls itself, like this:

int recurse(int number) {

    return (recurse(number));
}

由于局部变量和函数参数存储在堆栈中,因此它将填充堆栈并导致堆栈溢出.

Since local variables and function arguments are stored on the stack, then it will in fill the stack and cause a stack overflow.

大局部变量

如果尝试分配大量局部变量,则可以轻松地使堆栈溢出.这样的功能可能会导致问题:

If you try to allocate a large array of local variables then you can overflow the stack in one easy go. A function like this may cause the issue:

void hugeStack (void) {

    unsigned long long reallyBig[100000000][1000000000];

    ...
}

对此 查看全文

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