分段故障
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,所以checkbund函数中的条件必须是:
if(row < 0 || row >= board_width || col < 0 || col >= board_width)
我猜问题出在checkbund函数中。您可以执行以下操作:
row > board_width
我不知道你是怎么计算宽度的,但我相信你的问题就在这里。大小为3的矢量将是索引:myVec[0],myVec[1],myVec[2]。
如果在本例中,您说board_width为3,这将导致segfault。
相关文章:
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段故障 运行C++代码时出现 SIGSEGV
- 分段故障背包问题
- 分段故障 11,从类函数显示动态 C 字符串
- 面临分段故障 使用 ffmpeg 读取视频时,因为"pFormatCtx-> streams [i]-> codecpar"的地址0x00
- 在C++中,当指向删除和指向不同对象时,分段故障指针
- 对程序故障进行分段
- 分段故障说明
- 分段故障(核心转储)-不知道为什么
- 分段故障线程
- hiredis SET遇到分段故障
- 分段故障,合并排序算法
- 多线程程序中的分段故障和gdb回溯上的不完整信息
- 到达主C++之前分段故障
- 分段故障核心使用 IF流转储
- 使用向量的移动键盘排列(分段故障)
- 在二进制树插入和遍历期间,我得到了分段故障
- 分段故障在类之间返回整数