c++ 使用 *& as 参数(指针乐趣,三级树方法)
c++ use of *& as parameter (Pointer fun, tertiary tree method)
我正试图编写一个方法,将键值对添加到第三树,但我显然做错了,因为每当我到达标记的代码
时,我就会得到段错误void Tree::add(int k, Node *&r)
{
cout<<"add"<<endl;
if(r==NULL){
r = new Node(k);
//check(heap area);
}
开始问题代码
else if(r->keyCount == 1){
cout<<"adding second key";
if(r->getKey() < k){
Node * temp = new Node(r->getKey(),k,r->data[0],0);
delete r;
r = temp;
r->keyCount++;
cout<<"test"<<endl;
}
else
{
Node * temp = new Node(k,r->getKey(),0,r->data[0]);
delete r;
r = temp;
r->keyCount++;
cout<<"test"<<endl;
}
<<p> 结束代码/strong> }
else if(k < r->getKey())
{
cout<<"left"<<endl;
add(k,r->child[Node::L]);
}
else if(r->keyCount > 1 && k < r->getKey(1))
{
cout<<"middle"<<endl;
add(k,r->child[Node::M]);
}
else if(r->keyCount > 1 && k > r->getKey(1))
{
cout<<"right"<<endl;
add(k,r->child[Node::R]);
}
else
r = new Node(k);
}
我想做的是,在这个特定节点中只有1个键的情况下,用一个新的节点替换当前节点,这个节点的键在适当的地方(key[0]中的val较小,key[1]中的val较大)我该怎么做呢?
我的代码显然删除了旧节点的地址和指针,但没有正确地将指针重新分配给新节点。
编辑更新代码。输出如下:
% p4
Enter pairs consisting of an int and a double. I create a
ternary tree, keeping the data in order, by int. Finish entering
data by pressing ^d
2 2
add
Entering the pair: 2, 2
1 1
add
adding second key to current node
test
Entering the pair: 1, 1
-1 -1
add
left
add
Entering the pair: -1, -1
3 3
add
right
Segmentation Fault
编辑2 如果您想查看所有代码,这里有一个包含整个项目的zip文件的链接:http://sdrv.ms/WSrLfv
编辑3 更多错误数据——gdb在崩溃时的输出
Program received signal SIGSEGV, Segmentation fault.
0x08051628 in getData (x=@0x8047554) at testTree.cc:26
26 x[k]=d;
Current language: auto; currently c++
编辑4 通过GDB步进到segfault:
Breakpoint 1, Tree::add (this=0x8047554, k=3, r=@0x8047554) at tree.cc:58
58 cout<<"add"<<endl;
(gdb) n
add
61 if(r==NULL){
(gdb) n
65 else if(r->keyCount == 1){
(gdb) n
87 else if(k < r->getKey())
(gdb) n
92 else if(r->keyCount > 1 && k < r->getKey(1))
(gdb) n
97 else if(r->keyCount > 1 && k > r->getKey(1))
(gdb) n
99 cout<<"right"<<endl;
(gdb) n
right
100 add(k,r->child[Node::R]);
(gdb) n
Breakpoint 1, Tree::add (this=0x8047554, k=3, r=@0x806416c) at tree.cc:58
58 cout<<"add"<<endl;
(gdb) n
add
61 if(r==NULL){
(gdb) n
62 r = new Node(k);
(gdb) n
107 }
(gdb) n
107 }
(gdb) n
Tree::operator[] (this=0x8047554, index=3) at tree.cc:47
47 return *(locate(index,root)->data);
(gdb) n
48 }
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x08051628 in getData (x=@0x8047554) at testTree.cc:26
26 x[k]=d;
(gdb)
应该可以了。
回复你的编辑:我在你的输出中注意到一些有趣的东西:
-1 -1
add
left
add
Entering the pair: -1, -1
注意,由于递归调用,它先写了"left",然后又写了"add"。但是,在导致程序崩溃的输入中,您没有在
后面看到"add":3 3
add
right
Segmentation Fault
如果你看Tree::locate
函数:
Node * Tree::locate(int k, Node *rt) const
{
if(rt==NULL)
return rt;
if(k==rt->getKey())
return rt;
if(rt->keyCount>1 && k==rt->getKey(1))
return rt;
if(k < rt->getKey())
{
return locate(k,rt->child[Node::L]);
}
else if(rt->keyCount>1 && k < rt->getKey(1))
{
return locate(k,rt->child[Node::M]);
}
else if(rt->keyCount>1 && k<rt->getKey(1))
{
return locate(k,rt->child[Node::R]);
}
else
return NULL;
}
这条线:
else if(rt->keyCount>1 && k<rt->getKey(1))
与前一个条件相同,因此它被完全跳过。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- c++ 使用 *& as 参数(指针乐趣,三级树方法)