在 C++ 中构建后缀树
我正在尝试在 C++ 中构建后缀树,作为基因测序作业的一部分
void Tree::insert(string ins)
{
Node* iterator = chooseBranch(root, ins.at(0));
string temp;
for(int i=0; i<100; i++)
{
if(iterator->data=="")
.
.
.
chooseBranch()
是一个用于选择 4 个孩子中的哪一个的函数,我正在尝试检查是否该节点已经存在。我的 Node 类是:
struct Node{
Node();
string data;
Node* A;
Node* G;
Node* C;
Node* T;
};
这个 if 语句给了我一个段错误,我使用 gdb 回溯到:
#0 0x0000003ce249bbd6 in std::string::compare () from /usr/lib64/libstdc++.so.6
#1 0x000000000040185b in std::operator==<char, std::char_traits<char>, std::allocator<char> > ()
#2 0x0000000000401305 in Tree::insert ()
#3 0x00000000004016d4 in Tree::Tree ()
#4 0x00000000004010a2 in main ()
这种形式的 NULL 检查有什么问题/我还能如何检查 Node 是否没有数据?
I'm attempting to build a suffix tree in C++ as part of an assignment on gene sequencing
void Tree::insert(string ins)
{
Node* iterator = chooseBranch(root, ins.at(0));
string temp;
for(int i=0; i<100; i++)
{
if(iterator->data=="")
.
.
.
chooseBranch()
is a function to pick which of 4 children to go to, and I'm attempting to check if this Node already exists. My Node class is:
struct Node{
Node();
string data;
Node* A;
Node* G;
Node* C;
Node* T;
};
This if statement is giving me a segfault, which I used gdb to backtrack to:
#0 0x0000003ce249bbd6 in std::string::compare () from /usr/lib64/libstdc++.so.6
#1 0x000000000040185b in std::operator==<char, std::char_traits<char>, std::allocator<char> > ()
#2 0x0000000000401305 in Tree::insert ()
#3 0x00000000004016d4 in Tree::Tree ()
#4 0x00000000004010a2 in main ()
What is wrong with this form of NULL checking/how else could I check if the Node has no data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您根本没有检查指针
iterator
是否为NULL
,您只是取消引用它(如果它是NULL
,这将导致戏剧性的后果)代码>)。下面是一个可以尝试的示例,其中对
NULL
的检查已从for
循环中提升出来:It doesn't look like you are checking the pointer
iterator
forNULL
at all, you are simply dereferencing it (which will cause drama if it isNULL
).Here's a sample to try, with the check for
NULL
hoisted out of thefor
loop: