经典的堆栈溢出?
我是这个论坛的新手,而且我仍然是编程语言的业余爱好者,所以请善待我的任何愚蠢错误:p
我正在编写一个递归函数,它为搜索过程构建一个 kd 树。我在 Visual Studio '08 上使用 c 语言。处理几秒钟后,程序执行因错误而停止,即:
run_FAST_corner_detection.exe 中 0x77063de7 处出现未处理的异常: 0xC00000FD:堆栈溢出
现在,当代码中断时,指令附近有一个绿色箭头:
kd_node = malloc(sizeof(struct kd_node));
//this function allocates a pointer to a reserved memory of size struct kd_node.
这是内存不足的经典问题吗? 如何监控堆栈内存? (我知道这个问题已经被反复问过,但老实说我还没有找到好的方法来做到这一点)。
I am new to this forum and I am still an amateur in programming languages so please be kind with any of my silly mistakes :p
I am programming a recursive function which builds a kd-tree for a search process. I am using the c language on Visual Studio '08. After some seconds of processing, the program execution halts due to an error namely:
Unhandled exception at 0x77063de7 in run_FAST_corner_detection.exe:
0xC00000FD: Stack overflow
Now when the code breaks, there is a green arrow just near the instruction:
kd_node = malloc(sizeof(struct kd_node));
//this function allocates a pointer to a reserved memory of size struct kd_node.
Is this the classical problem of running out of memory?
How can I monitor the stack memory? (I know that this question has been asked repeatedly but honestly I have yet found no good method to do this).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,堆栈溢出可能是由于您在递归深处调用 malloc 造成的。对 malloc 的调用会将返回地址甚至参数推送到堆栈上,这可能会导致堆栈溢出。不要在代码中进行递归 - 尝试使代码迭代(改为使用循环)。当递归不受限制时尤其如此。
Well, the stack overflow might be due to you calling malloc while deep in the recursion. The call to malloc pushes the return address and perhaps even parameters on the stack and this might be the thing which causes the stack to overflow. Don't do recursions in your code - try to make the code iterative (with loops instead). This is especially true for when the recursion is not bounded.
要监视堆栈使用情况,只需获取任何局部变量(在函数中定义的变量)的地址,并将其与主函数(或线程入口函数)中的局部变量的地址进行比较:
要减少堆栈使用,可以限制递归,或者避免使用大型局部变量(例如结构体、数组)并且不要使用
alloca
。您可以用指向动态分配的堆内存的指针替换大型局部变量(但不要忘记释放它!)To monitor stack usage, simply take the address of any local variable (one defined within a function), and compare it against the address of a local variable in your main function (or thread entry function):
To reduce stack usage, either limit recursion, or avoid using large local variables (such as structs, arrays) and don't use
alloca
. You can replace large local variable with pointers to dynamically allocated heap memory (but don't forget to free it!)