增加堆栈保留后堆栈溢出
我有一个作业,我需要用 C 中分配的数组基本上填充主内存。我正在使用 VS2010 并不断收到堆栈溢出错误。将堆栈保留量增加到超过默认的 1 MB 会有所帮助,但是现在我正在使用的数组大小更大,而且似乎无论我增加多少保留量,它现在都会不断地给我带来堆栈溢出错误。 任何帮助将不胜感激。 -谢谢
I have an assignment where I need to basically fill up main memory with allocated arrays in C. I am using VS2010 and kept on receiving stack overflow errors. Increasing the stack reserve past its default 1 MB helped, however now the array size I am working with is even larger and it seems no matter how much I increase the reserve, it now continually gives me a stack overflow error.
any help would be appreciated.
-thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能正在堆栈上分配数组。这就是为什么你会遇到堆栈溢出,因为你的堆栈永远不会像你的整个主内存一样大。
您需要使用
malloc()
在堆上创建数组。这将使您用尽所有主内存。换句话说,你不能这样做:
那肯定会耗尽你的筹码。你需要这样做:
并且你最终需要像这样释放它:
否则你将得到内存泄漏。
You're probably allocating your arrays on the stack. That's why you're getting stack overflows since your stack is never gonna be as large as your entire main memory.
You need to use
malloc()
to create arrays on the heap. That will allow you to use up all the main memory.In other words, you can't do this:
That will certainly blow your stack. You need to do this:
and you need to eventually free it like this:
Otherwise you will get a memory leak.