简体   繁体   English

运行时错误(堆栈溢出)

[英]runtime error (stack overflow)

I've got this code in C language: 我已经用C语言获得了以下代码:

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

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. 这里的窍门是使用堆分配(因为在大多数平台上,至少在我听说过的所有平台上),堆要比堆栈大得多。

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). 要在堆上分配内存,请使用malloc (此外,使用完malloc ,请不要忘记使用free释放内存,否则会泄漏内存)。

EDIT: 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. 在这一点上,您实际上部分还可以。

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 ). 您将scanf调用点指向一个char数组(这是问题),并在数组k中一个int(没问题。所以现在,options数组中的所有条目都指向无处/任何地方。您需要为其分配内存(再次使用malloc )。

As for strdup it allocates the memory itself and returns the pointer, again no problem here. 至于strdup,它会分配内存本身并返回指针,这同样没有问题。 Just don't forget to free it after you're done using it because again this would be a memory leak. 只是在使用完后不要忘记释放它,因为这又会导致内存泄漏。

char *options[100000] allocates 100000 string pointers, not strings. char * options [100000]分配100000字符串指针,而不是字符串。 scanf is being passed gibberish. scanf正在传递胡言乱语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM