CPP_TEST.exe中0x7604c128处的未处理异常:0xC00000FD:堆栈溢出

Ras*_*yak 0 c c++ malloc free

为什么stackover flow会出现在我正在使用的地方fflushfree我的代码中.请帮我.

using namespace std;

    struct abc{
        int x;int y;
        }abc;

int _tmain(int argc, _TCHAR* argv[])
{
    struct abc *xyz = (struct abc *) malloc(sizeof(struct abc));
    xyz->x = 5;
    printf("%d\n", xyz->x);
    //system("pause");
        free(xyz);
     // xyz = NULL;
        fflush(stdout);
        _tmain(NULL, NULL);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑代码:

xyz:
    struct abc *xyz = (struct abc *) malloc(sizeof(struct abc));
    xyz->x = 5;
    printf("%d\n", xyz->x);
    //system("pause");

    free(xyz);
    xyz = NULL;
    fflush(stdout);
    goto xyz;
Run Code Online (Sandbox Code Playgroud)

Mar*_*mer 8

您无条件地_tmain()_tmain()函数中调用函数,从而导致无限递归.每次调用都需要在堆栈上分配空间(永远不会释放),从而导致StackOverflow异常.看看例如维基百科也完全理解这个问题.

除此之外,main()从你自己的代码调用函数通常不是一个好主意,因为它被标准禁止.

§3.6.1.3:函数main不得在程序中使用.

你为什么要这么做?

  • 指定"几乎";)从您自己的程序调用main函数是未指定的行为.无限递归将导致堆栈溢出,除非编译器能够将其转换为循环.但我不会依赖任何一种行为,因为它可能会从编译器变为编译器. (2认同)