2013-07-10 31 views
0

我有一些代码应该递归地覆盖在一个麻烦牌上的所有位置。但是由于某些原因,它找到只有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 
+0

请不要写'== true'。 – Ryan

+0

你会提出什么建议,而不是@minitech – wawiti

+2

@wawiti,s/== true // – chris

回答

2

什么board_width的价值?如果这是3,那么你忘了索引从0到最大可用指数开始是2,所以在checkbound功能的条件必须是:

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

这是正确的答案,我想。这行在你的board_width = sqrt(boardcnt.size());可能会给这个边缘案例错误。 – sukunrt

+0

这绝对是正确的答案!非常感谢,我感到荒谬,我没有抓住那个。 – wawiti

1

我的猜测是,你的问题是在你的checkbound功能。你做以下事情:

row > board_width 

我不知道你如何计算宽度,但我相信你的问题在这里。大小为3的向量将是索引: myVec [0],myVec [1],myVec [2]。

如果在这个例子中,你说board_width是3,这会导致你的段错误。