递归 main() - 为什么会出现段错误? [英] Recursive main() - why does it segfault?

查看:26
本文介绍了递归 main() - 为什么会出现段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的程序会出现段错误?

Why does the following program segfault?

int main() { main(); }

即使它是一个没有结束的递归,因此根据定义是无效的,但我不明白它为什么会出现段错误(gcc 4.4.3 和 clang 1.5(主干)).

Even though it is a recursion that does not end and is therefore invalid by definition, I don't see why it segfaults (gcc 4.4.3 and clang 1.5 (trunk)).

推荐答案

因为每次调用自己都会分配一点栈空间;最终它耗尽了堆栈空间和段错误.不过,我有点惊讶它会出现段错误.我会期待(鼓)堆栈溢出

Because every time it calls itself it allocates a little bit of stack space; eventually it runs out of stack space and segfaults. I'm a bit surprised it goes with a segfault, though; I would have expected (drum roll) stack overflow!

这篇关于递归 main() - 为什么会出现段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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