runtime error (stack overflow)

为君一笑 提交于 2019-11-27 09:51:28

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).

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).

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.

char *options[100000] allocates 100000 string pointers, not strings. scanf is being passed gibberish.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!