【发布时间】:2013-04-15 09:23:08
【问题描述】:
我编写了一个 C++ 代码来构建二叉搜索树。代码编译正确,但是当我尝试运行可执行文件时,它给出了分段错误。以下是我的代码:
#include <iostream>
using namespace std;
struct Node{
Node *parent, *right, *left;
int data;
};
class bst{
private:
Node* root;
public:
bst (int data);
void insert (Node * node, int data);
void insert (int data);
Node * search (int data);
Node * search (Node * node, int data);
// remove (Node * node, int data);
};
bst::bst (int data){
root -> data = data;
}
void bst::insert (Node * node, int data){
if (node == NULL){
node -> data = data;
}
else if (data < node -> data){
insert (node -> left, data);
}
else if (data > node -> data){
insert (node -> right, data);
}
}
void bst::insert (int data){
insert (root, data);
}
Node * bst::search (Node * node, int data){
if (node == NULL || node -> data == data)
return node;
else if (data < node -> data)
return search (node -> left, data);
else if (data > node -> data)
return search (node -> right, data);
}
Node * bst::search (int data){
search (root, data);
}
int main(){
cout << "main entry\n";
bst __bst (10);
cout << "new causing problem\n";
bst bst2(1);
// bst *my_bst = new bst(10);
cout << "tree created\n";
/* my_bst -> insert(32);
my_bst -> insert(3);
my_bst -> insert(36);
my_bst -> insert(93);
my_bst -> insert(23);
cout << "insertion completed\n";
if (my_bst -> search(4) == NULL )
cout << "4 does not exist\n";
if (my_bst -> search(36) != NULL)
cout << "36 exists in tree\n";
*/ return 0;
}
在调试问题时,我发现了一个有趣的观察结果。当 main 函数仅包含 bst __bst (10); 对象定义和之后的 "new cause problem\n" 注释时,它不会打印任何内容。所以我假设第一个对象定义引起了问题。但是当我把 bst bst2(1); 或 bst *my_bst = new bst(10); 放在主要位置时,分段错误再次发生但不是在 之前打印新的导致问题\n"。所以执行确实达到了这一点。所以在这种情况下,第一个对象定义没有造成任何问题。
谁能说出导致分段错误的原因以及为什么会发生这种奇怪的事情。
编辑: 正如你们所有人指出的那样,我试图在构造函数代码期间取消引用 NULL 指针,我做了建议的更改并且代码运行良好。但是我有一个类似的程序,即使没有这些更改也能正常工作。下面是代码:
#include <iostream>
using namespace std;
struct Name{
string first_name;
string last_name;
};
class Person{
public:
Person (string first_name, string last_name){
name -> first_name = first_name;
name -> last_name = last_name;
}
int get_age(){
return age;
}
void set_age (int new_age){
age = new_age;
}
string get_first_name(){
return name -> first_name;
}
string get_last_name(){
return name -> last_name;
}
private:
Name * name;
int age;
};
int main (){
Person prince ("Prince", "Kumar");
cout << prince.get_first_name() << endl;
cout << prince.get_age() << endl;
return 0;
}
在这种情况下,程序运行良好。 Name 的默认构造函数是否为 name 指针分配了一些空间。我知道它将 0 值分配给数字类型,将 NULL 分配给指针、对象和字符串。我说的对吗?
【问题讨论】:
-
旁注:在 C++ 中,任何包含
__或以_开头后跟大写字母的标识符都是保留的。你不应该使用它们。 -
不要忘记刷新流(
std::flush或std::endl),否则无法保证您会真正看到任何输出。 -
@sftrabbit,我相信
\n已经做到了。 -
@Shahbaz:你想到了
endl。流应在其析构函数中刷新。 -
@Wug
'\n'可能会或可能不会刷新标准输出(它的实现已定义),但endl保证将刷新输出。这就是'\n'和endl之间的区别。
标签: c++ pointers constructor segmentation-fault