运行时错误(堆栈溢出) [英] runtime error (stack overflow)

查看:333
本文介绍了运行时错误(堆栈溢出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C语言这个code:

I've got this code in C language:

char *options[100000];
int k[100000];
char *param[100000];
int n;
int i,j;
...
scanf("%d",&n);
for (i=0;i<n;i++)
{   
    scanf("%s%d",&options[i],&k[i]);
    param[i]="On";
}
...

就像PROGRAMM了这一点:

just as the programm reaches this point:

scanf("%s%d",&options[i],&k[i]);

我得到的运行时错误(堆栈溢出)。
这里的输入应该是这样的:

I get the runtime error (stack overflow). The input here should be like this:

word1 number1
word2 number2

和等。我有不知道为什么会这样。有什么问题?

and so on. I've got no idea why is this happening. What's the problem?

推荐答案

好吧......所以我想会有人能解答你的堆栈溢出的问题,但到目前为止,大家后来才提到你真正遇到了问题(更多内容)是无关的堆栈溢出(这将是有问题的,但只有当你解决这个问题在前)。

Ok... so I thought someone would provide an answer to your stack overflow problem but so far everybody only mentioned a problem you actually have (more on this later) that is unrelated to the stack overflow (it'll be problematic but only once you fix this first).

-----------------
|  Your stack   |
| (grows down)  |
|               |
-----------------
|               |
|               |
|               |
|               |
|               |
|               | -- max stack size is here
|               |
|               |
|               |
|               |
|               |
-----------------
|   Your heap   |
|   (grows up)  |
|               |
-----------------

和则尝试分配一帮真正的大阵列和运行的空间。

And then you try to allocate a bunch of really big arrays and run out of space

-----------------
|  Your stack   |
| (grows down)  |
|               |
|               |
|               |
|               |
|               |
|               |
|               |
|               | -- max stack size is here
|               |
----------------- -- what you actually need
|               |
|               |
|               |
|               |
-----------------
|   Your heap   |
|   (grows up)  |
|               |
-----------------

所以,你得到一个运行时错误(堆栈溢出),因为你已经尝试使用更多的堆栈空间比你有什么可用

So you get a run-time error (stack overflow) because you've tried to use more stack space than what you have available.

这里的技巧是使用堆分配(因为在大多数平台上,至少我听到的那些)的堆是大量比栈大。

The trick here is to use heap allocation (because on most platforms, at least all the ones I've heard of) the heap is massively bigger than the stack.

要在您使用的malloc (另,当你用它做,不要忘记使用来释放内存堆上分配内存免费,否则你会泄漏内存)。

To allocate memory on the heap you use malloc (also, when you're done with it don't forget to release the memory using free, or else you'll leak the memory).

编辑:
奖励:你有其他的问题。其他的答案似乎表明您正在访问/解引用/使用不是分配的内存。你实际上部分罚款这一点。

Bonus: The other problem you have. Other answers seem to indicate you're access/dereferencing/using memory that's not allocated. You're partially actually fine on this point.

您scanf函数调用指向一个字符数组(这里的问题),并在数组K(没问题的一个int。所以现在在选项阵列点的所有条目无处/任何地方。你需要为他们分配内存(再次使用的malloc )。

You scanf call point to a char array (here's the problem) and an int in the array k (no problem. So right now all the entries in the options array point to nowhere/anywhere. You need to allocate memory for them (again using malloc).

至于它的strdup分配内存本身并返回指针,在这里再次没有问题。只是不要忘了释放它你使用它,因为这同样会是一个内存泄漏后完成。

As for strdup it allocates the memory itself and returns the pointer, again no problem here. Just don't forget to free it after you're done using it because again this would be a memory leak.

这篇关于运行时错误(堆栈溢出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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