[英]Seg Fault at function call
我正在嘗試編寫一個程序,該程序將對樹執行不同的功能,到目前為止,除了打印功能外,所有功能都可以工作。 它以前可以工作,但是在嘗試解決其他功能中的一些問題時(沒有弄亂它),現在它們已修復,這個突然不起作用,我無法理解為什么。 這是我的代碼:
主.cpp:
using namespace std;
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "lcrs.h"
int main()
{
char *temp1;
char *temp2;
temp1 = new char;
temp2 = new char;
lcrs tree;
do{
cout << "LCRS> ";
cin >> temp1;
if(strcmp(temp1, "quit") == 0)
{
return 0;
}
if(strcmp(temp1, "insert") == 0)
{ cin >> temp2;
bool error;
for(int i=0; i<strlen(temp2); i++)
{
if(!isdigit(temp2[i]))
{
cout << "Error!" << endl;
error = true;
}
}
if(!error)
{
tree.insert(atoi(temp2), tree.root);
}
}
else if(strcmp(temp1, "height") == 0)
{
if(tree.root == NULL)
cout << "-1" << endl;
else
cout << tree.getHeight(tree.root) << endl;
}
else if(strcmp(temp1, "preorder") == 0)
{
cout << "Root is " << tree.root->data << endl;
tree.print(tree.root);
cout << "" << endl;
}
else if(strcmp(temp1, "search") == 0)
{
cin >> temp2;
bool error;
for(int i=0; i<strlen(temp2); i++)
{
if(!isdigit(temp2[i]))
{
cout << "Error!" << endl;
error = true;
}
}
if(!error)
{
if(tree.search(atoi(temp2), tree.root))
cout << "true" << endl;
else
cout << "false" << endl;
}
}
else
{
cout << "Error! " << endl;
}
}while(strcmp(temp1, "quit") !=0);
return 0;
}
lcr.h:
using namespace std;
#include <cstdlib>
#include <iostream>
class node{
public:
int data;
node *right;
node *below;
node()
{
right = NULL;
below = NULL;
}
};
class lcrs{
public:
node *root;
bool search(int, node*);
void print(node*);
void insert(int, node*&);
int getHeight(node*);
lcrs()
{
root = NULL;
}
};
lcrs.cpp:
using namespace std;
#include "lcrs.h"
bool lcrs::search(int x, node *b)
{
if(b == NULL)
return false;
else
{
if(b->data == x)
return true;
else
{
return search(x, b->right) || search(x, b->below);
}
}
}
void lcrs::print(node *z)
{
if(z->below == NULL || z->right != NULL)
{
cout << z->data << ",";
print(z->right);
}
else if(z->below != NULL && z->right == NULL)
{
cout << z->data << ",";
print(z->below);
}
else if(z->below != NULL && z->right != NULL)
{
cout << z->data << ",";
print(z->below);
print(z->right);
}
else if(z->right == NULL && z->below == NULL)
{
cout << z->data << "";
}
}
void lcrs::insert(int x, node *&a)
{
if(a == NULL)
{
node *newnode;
newnode = new node;
newnode->data = x;
a = newnode;
}
else if(a->data < x)
{
if(a->right != NULL)
{
insert(x, a->right);
}
else if(a->below != NULL)
{
if(a->below->right != NULL)
{
insert(x, a->below->right);
}
else
{
insert(x, a->below);
}
}
else
{
node *n;
n = new node;
n->data = x;
a->below = n;
}
}
else if(a->data > x)
{
if(a->below != NULL)
{
insert(x, a->below);
}
else
{
node *n;
n = new node;
n->data = x;
a->right = n;
}
}
}
int lcrs::getHeight(node *h)
{
int height = 0;
node *n;
n = new node;
n = h;
while(n->below != NULL || n->right != NULL)
{
if(n->below != NULL)
{
n = n->below;
height ++;
}
else if(n->right != NULL)
{
n = n->right;
}
}
return height;
}
我在tree.print(tree.root)函數調用中遇到了段錯誤。 我在函數的開頭放了一個打印語句,但它從來沒有做到這一點,所以我對問題出在哪里感到有些困惑。
非常感謝您的幫助。
有很多問題。 它可能與您讀取輸入的方式有關,將字符串填充到包含單個字符的緩沖區中(請改用 std::string - 它的存在是有原因的),或者它可能與樹的事實有關。 root 可能為空並且您正在取消引用它: cout << "Root is " << tree.root->data << endl;
此外,您真的應該在發布代碼時嘗試減少內容。 這有兩個目的:它可以幫助您(因為您實際上可能自己發現出了什么問題,或者至少可以隔離錯誤,因為您正在整理內容)並且它可以幫助我們,因為我們不需要瀏覽頁面和代碼頁。
我找到了問題所在,這只是一個小錯誤。 (當然是。)感謝所有給出合理答案並真誠地嘗試提供幫助的人。 還有,感謝所有給我sass的人。 我很感激在我使用了調試器並且仍然不知所措之后,人們可以提醒我,我只是一個低水平的 ComSci 學生。 非常感謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.