c++ - 分段故障?

标签 c++ recursion segmentation-fault

我有一些代码应该递归地覆盖棋盘上的所有位置。但出于某种原因,它在仅找到 2 个单词后会抛出段错误?知道为什么会发生这种情况吗?

代码可能会抛出错误:

//++++++++++++ Find Word ++++++++++++++//
void findword(vector<vector<tile> > board, unsigned int row, unsigned int col, set<string> &results, unsigned int board_width, string word, set<string> dictionary)
{               
    if(checkbound(row, col, board_width) == false)          // If tile is outside of the boundary than do nothing
        return;
    if(board[row][col].getused() == true)               // If tile has already been used than do nothing
        return;
    word = word + board[row][col].getvalue();           // Adds letter to word

    //Check Prefixes
    set<string>::iterator pos;
    pos = dictionary.lower_bound(word);             // Creates an iterator at position
    if(pos== dictionary.end())                  // If it reaches the end without finding word than do nothing
    {
        return;

    }
    else if(word == pos->substr(0,word.length()))
    {
        cout<<"word: " <<word<<endl;
        cout<<"dict: " <<*pos<<endl;
        if(word == *pos)                    // If word = word at prefix
        {       
            cout<< word<<" word inserted"<<endl;    
            results.insert(word);               // Add words to results set
        }
    }
    else
        return;

    //set to used
    board[row][col].setused(true);                  // set tile to used




    findword(board, row-1, col-1, results, board_width, word, dictionary);  // Checks all tiles around every tile
    findword(board, row-1, col, results, board_width, word, dictionary);
    findword(board, row-1, col+1, results, board_width, word, dictionary);
    findword(board, row, col-1, results, board_width, word, dictionary);
    findword(board, row, col+1, results, board_width, word, dictionary);
    findword(board, row+1, col-1, results, board_width, word, dictionary);
    findword(board, row+1, col, results, board_width, word, dictionary);
    findword(board, row+1, col+1, results, board_width, word, dictionary);

    board[row][col].setused(false);                 // set tile to not-used
}

给出的错误:

word: r
dict: riot
word: ro
dict: robot
word: rob
dict: robot
word: robo
dict: robot
word: robot
dict: robot
robot word inserted
word: roo
dict: root
word: root
dict: root
root word inserted
Segmentation fault (core dumped)

Valgrind的主要错误代码:

==4629== Invalid read of size 1
==4629==    at 0x407C2E: tile::getused() (tile.cpp:33)
==4629==    by 0x401ACE: findword(std::vector<std::vector<tile, std::allocator<tile> >, std::allocator<std::vector<tile, std::allocator<tile> > > >, unsigned int, unsigned int, std::set<std::string, std::less<std::string>, std::allocator<std::string> >&, unsigned int, std::string, std::set<std::string, std::less<std::string>, std::allocator<std::string> >) (main.cpp:58)
==4629==    by 0x4020EC: findword(std::vector<std::vector<tile, std::allocator<tile> >, std::allocator<std::vector<tile, std::allocator<tile> > > >, unsigned int, unsigned int, std::set<std::string, std::less<std::string>, std::allocator<std::string> >&, unsigned int, std::string, std::set<std::string, std::less<std::string>, std::allocator<std::string> >) (main.cpp:93)
==4629==    by 0x4020EC: findword(std::vector<std::vector<tile, std::allocator<tile> >, std::allocator<std::vector<tile, std::allocator<tile> > > >, unsigned int, unsigned int, std::set<std::string, std::less<std::string>, std::allocator<std::string> >&, unsigned int, std::string, std::set<std::string, std::less<std::string>, std::allocator<std::string> >) (main.cpp:93)
==4629==    by 0x401F78: findword(std::vector<std::vector<tile, std::allocator<tile> >, std::allocator<std::vector<tile, std::allocator<tile> > > >, unsigned int, unsigned int, std::set<std::string, std::less<std::string>, std::allocator<std::string> >&, unsigned int, std::string, std::set<std::string, std::less<std::string>, std::allocator<std::string> >) (main.cpp:91)
==4629==    by 0x402264: findword(std::vector<std::vector<tile, std::allocator<tile> >, std::allocator<std::vector<tile, std::allocator<tile> > > >, unsigned int, unsigned int, std::set<std::string, std::less<std::string>, std::allocator<std::string> >&, unsigned int, std::string, std::set<std::string, std::less<std::string>, std::allocator<std::string> >) (main.cpp:95)
==4629==    by 0x402BF0: main (main.cpp:185)
==4629==  Address 0x4c3b178 is 8 bytes after a block of size 48 alloc'd

Checkbound 函数:

//+++++++++++ Check Bounds ++++++++++++//
bool checkbound(unsigned int row, unsigned int col, unsigned int board_width)
{
    if(row < 0 || row > board_width || col < 0 || col > board_width)
        return false;
    else
        return true;
}

董事会:

r b o
o i t
r o h

最佳答案

board_width 的值是多少?如果这是 3,那么你忘记了索引从 0 开始并且最大可用索引是 2,所以 checkbound 函数中的条件必须是:

    if(row < 0 || row >= board_width || col < 0 || col >= board_width)

关于c++ - 分段故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17562704/

相关文章:

c++ - opencv C++ 中的 findHomography(par1, par2, par3) 返回什么

c++ - C++11 核心语言是否处理 Singleton Dead Reference?

python - python中无替换的Word Ladder

c# - 跨线程事件接收器

C++ 自定义类未被其他类识别

recursion - 执行递归lisp函数时堆栈溢出

javascript - IE 递归中的堆栈溢出 -> window.frames 不等于自身

c++ - 为什么 V8 的 Hello World 在 Ubuntu 上会导致段错误?

c++ - 创建共享库时检测到段错误/glibc

c - 使用带有 * char 的 strcmp 的段错误