用递归关系求1!+2!+……+n! 从4开始,运行时就停止工作,为什么啊?

实现 输入n,求1!+2!+……+n!=?

小白一只,请教各位,下面这个程序,输入1,2,3都没问题,输入>4的数运行后就停止工作,这是为什么啊?代码有什么问题吗?

#include "stdafx.h"
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int add(int x);
    int f(int m);
    int a,b;
    printf("input an integer:");
    scanf("%d",&a);
    b=add(a);
    printf("output:%d",b);
    return 0;
}
int add(int x)
{
    int f(int m);
    int y;
    if(x==1)
        y=1;
    else y=f(x)+add(f(x-1));
    return(y);
}
int f(int m)
{
    int n;
    if(m==1)
        n=1;
    else n=f(m-1)*m;
    return(n);
}



提问者是按递归的思路来实现阶乘的,所以每有一次阶乘,都需要调用很多次f,随着你输入的n越来越大,极大量的函数调用入栈会占用特别多的资源,导致最终一直在入栈没有出栈,因为只有最底层的f运行完,才会一个接着一个出栈,所有最终内存提供资源崩溃时就无法执行了,由保集成环境的护机制强制退出

只有入栈没有出栈,只有底层运行完才会出栈