Why infinite recursion leads to seg fault

后端 未结 8 2220
长情又很酷
长情又很酷 2020-12-04 01:42

Why infinite recursion leads to seg fault ? Why stack overflow leads to seg fault. I am looking for detailed explanation.

int f()
{
  f();
}

int main()
{
           


        
8条回答
  •  盖世英雄少女心
    2020-12-04 02:14

    Every time you call f(), you increase the size of the stack - that's where the return address is stored so the program knows where to go to when f() completes. As you never exit f(), the stack is going to increase by at least one return address each call. Once the stack segment is full up, you get a segfault error. You'll get similar results in every OS.

提交回复
热议问题