我在程序中遇到堆栈溢出异常C++

I'm getting a stack overflow exception in C++ program

本文关键字:栈溢出 异常 C++ 堆栈 遇到 程序      更新时间:2023-10-16

我得到了一个system.stackoverflowexception。

我认为它发生在insert()函数内部的某个地方。

void insert(char* word){
    int r1 = rand()%x;         // Here is where I suspect the problem starts
    int c1 = rand()%x;
    if(gameBoard[r1][c1]=="") {
        gameBoard[r1][c1] = word;
        insertWordCopy(word);
    } else 
        insert(word);
    }

以下内容不正确,因为它比较了指针:

if(gameBoard[r2][c2]=="") {

因此,代码几乎肯定总是沿着else分支向下,从而导致无限递归。