我正在尝试用 C++ 实现哈希表数据结构,但每次运行该程序时,我都会在第 86 行中遇到运行时错误(SIGSEGV,段错误),如下所示。
i.e.:
putInHash(str,hashTable,m); in main().
这是我的代码:
#include <iostream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
typedef struct node
{
struct node *next,*prev;
string data;
}node;
int hashCode(string str)
{
char arr[str.size()+1];
strcpy(arr,str.c_str());
int code=0;
for(int i=0;i<str.size();i++)
{
code+=((i+1)*((int)arr[i]));
}
return code;
}
int compress(int k,int m)
{
double a=(sqrt(5.0)-1)/2;
return floor(m*(fmod(k*a,1)));
}
void display(node* hashTable[],int m)
{
for(int i=0;i<m;i++)
{
cout<<i<<":\n";
node* p=hashTable[i];
while(p!=NULL)
{
cout<<p->data<<" , ";
}
cout<<"\n";
}
}
void putInHash(string str,node* hashTable[],int m)
{
int k=hashCode(str);
int bucket=compress(k,m);
if(hashTable[bucket]==NULL)
{
hashTable[bucket]=(node*)malloc(sizeof(node));
hashTable[bucket]->prev=NULL;
hashTable[bucket]->next=NULL;
hashTable[bucket]->data=str;
}
else
{
node* temp=(node*)malloc(sizeof(node));
temp->data=str;
temp->next=hashTable[bucket];
hashTable[bucket]->prev=temp;
temp->prev=NULL;
hashTable[bucket]=temp;
}
}
int main()
{
cout<<"Enter number of strings to add in hash table: ";
long int n;
cin>>n;
cout<<"\n";
int m=13;
node* hashTable[m];
for(int i=0;i<m;i++)
{
hashTable[i]=NULL;
}
string str;
cout<<"Enter the strings:\n";
for(int i=0;i<n;i++)
{
cin>>str;
putInHash(str,hashTable,m);
}
display(hashTable,m);
return 0;
}
我以为可能是因为传了字符串,结果发现不是这样
有人可以指导我完成它吗?
我认为错误可能在于将 hashTable[]
作为参数传递。
最佳答案
我无法重现您的问题(我在 Linux 平台上使用 clang++,我想您的问题与平台有关)但我看到了一些可以解释它的东西。
您使用 malloc()
为其中包含 std::string
的结构分配内存。
这很糟糕。
真的,真的很糟糕。
因为malloc()
可以分配内存但不能在其中构造data
成员。
在 C++ 中你应该使用 new
;至少,分配不平凡的对象(std::string
不平凡)。
// node* temp=(node*)malloc(sizeof(node)); // DANGEROUS
node * temp = new node;
这是导致 sigsegv 的问题(我想),但您的代码还有很多其他问题。
示例:display()
中的while
进入循环,因为p
保持不变;你应该这样改变display()
void display (node * hashTable[], int m)
{
node * p;
for(int i=0;i<m;i++)
{
cout << i << ":\n";
for ( p = hashTable[i] ; p ; p = p->next )
cout << p->data << " , ";
cout << "\n";
}
}
另一个重点:可变长度数组不是 C++;它是 C (C99)。所以这几行是错误的
char arr[str.size()+1];
node* hashTable[m];
你不需要第一个(绝对没用),你可以用这种方式简化 hashcode()
(请尽可能通过 const 引用传递字符串)
int hashCode (const string & str)
{
int code = 0;
for ( int i = 0 ; i < str.size() ; ++i )
code += (i+1) * int(str[i]);
return code;
}
关于hashTable
,你可以用std::vector
代替它
// node* hashTable[m]; no C++
//for(int i=0;i<m;i++) // useless
//{ // useless
// hashTable[i]=NULL; // useless
//} // useless
std::vector<node *> hashTable(m, NULL); // m NULL node pointers
显然,putInHash()
应该是
void putInHash (string str, std::vector<node*> & hashTable, int m)
和display()
void display (const std::vector<node*> & hashTable, int m)
并记得释放分配的内存。
p.s.:抱歉我的英语不好。
--- 编辑 ---
phonetagger 是对的:删除内存(链接节点的 vector )并非易事。
我建议像下面这样的函数
void recursiveFreeNode (node * & nd)
{
if ( nd )
{
recursiveFreeNode(nd->next);
delete nd; // added with EDIT 2; sorry
nd = NULL; // useless, in this, case, but good practice
}
}
并在 display()
调用之后在 main()
中调用它(对于 vector 的每个节点)
for ( unsigned ui = 0U ; ui < hashTable.size() ; ++ui )
recursiveFreeNode(hashTable[ui]);
--- 编辑 2 ---
抱歉:我忘记了更重要的一行:delete node
(感谢 phonetagger)。
根据phonetagger的其他建议,我提出了一个非递归函数来删除哈希表的节点
void loopFreeNode (node * & nd)
{
node * tmp;
for ( ; nd ; nd = tmp )
{
tmp = nd->next;
delete nd;
}
nd = NULL;
}
显然 for
循环,要使用 loopFreeNode()
,应该是
for ( unsigned ui = 0U ; ui < hashTable.size() ; ++ui )
loopFreeNode(hashTable[ui]);
关于c++ - C++实现哈希表时出现SIGSEGV错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36751568/