我编译了一次文件,并使用相同的输出运行它,并且我只有大约20-30%的时间得到分段错误。我只是通过电子邮件发送给我的教授,但她正在服用。我真的不知道为什么,特别是因为错误不会一直发生,但有时只有完全相同的a.out文件。我确切地知道错误发生的位置,来自if(sDB [index] == 0)。
感谢您的帮助,
如果需要更多代码来解决问题,请告诉我。
* sDB是我的构造函数中初始化的指针数组:
*sDB = new HashElem[MAX];
结构:
struct Elem {
student *info;
Elem *next;
};
struct HashElem {
student *info;
HashElem *next;
};
我的代码片段:
void studentsDB::push( student *std ) {
Elem *e = new Elem;
e->info = std;
Elem *cur = head;
while( cur->next != 0 )
cur = cur->next;
cur->next = e;
e->next = 0;
int index = std->hash( );
HashElem *h = new HashElem;
h->info = std;;
if( sDB[index] == 0 ) { //<<< THIS LINE CAUSES THE ERROR
sDB[index] = h;
h->next = 0;
}
else {
HashElem *ha = sDB[index];
while( ha->next != 0 ) {
ha = ha->next;
}
ha->next = h;
}
size++;
}
散列():
int student::hash( ) {
int ret = 0;
string s = id;
for( int i = 0; i < s.length( ); i++ )
ret = 33 * ret + s[i];
return ret % MAX;
}
输出:
bash-4.2$ g++ *.cpp
bash-4.2$ ./a.out students.dat courses.dat
begin
objects created
+++ Before the if statement +++
+++ 548 +++
Segmentation fault
bash-4.2$ ./a.out students.dat courses.dat
begin
objects created
+++ Before the if statement +++
+++ 548 +++
Segmentation fault
bash-4.2$ ./a.out students.dat courses.dat
begin
objects created
+++ Before the if statement +++
+++ 548 +++
+++ After +++
+++ Before the if statement +++
+++ 626 +++
+++ After +++
+++ Before the if statement +++
+++ 605 +++
+++ After +++
+++ Before the if statement +++
+++ 915 +++
+++ After +++
+++ Before the if statement +++
+++ 915 +++
printList
Flintstone, Fred 000-12SA 3 121314 12333 12116
Flintstone, Wilma 000-45SA 2 12332 12111
Glotz, Joe Q 901-9984 3 12332 12116 12111
Rubble, Barney 001-01SA 3 121314 12111 12116
CS001 1 12111 1 10 3 000-45SA 901-9984 001-01SA
CS515 2 121314 4 45 2 000-12SA 001-01SA
CH302 1 12116 5 15 3 000-12SA 901-9984 001-01SA
MA111 1 12333 4 15 1 000-12SA
PH999 1 12999 2 10 0000-12SA901-9984
PY000 3 12332 6 5 2 000-45SA 901-9984
bash-4.2$ ./a.out students.dat courses.dat
begin
objects created
+++ Before the if statement +++
+++ 548 +++
Segmentation fault
bash-4.2$ ./a.out students.dat courses.dat
begin
objects created
+++ Before the if statement +++
+++ 548 +++
Segmentation fault
bash-4.2$ ./a.out students.dat courses.dat
begin
objects created
+++ Before the if statement +++
+++ 548 +++
+++ After +++
+++ Before the if statement +++
+++ 626 +++
+++ After +++
+++ Before the if statement +++
+++ 605 +++
+++ After +++
+++ Before the if statement +++
+++ 915 +++
+++ After +++
+++ Before the if statement +++
+++ 915 +++
printList
Flintstone, Fred 000-12SA 3 121314 12333 12116
Flintstone, Wilma 000-45SA 2 12332 12111
Glotz, Joe Q 901-9984 3 12332 12116 12111
Rubble, Barney 001-01SA 3 121314 12111 12116
CS001 1 12111 1 10 3 000-45SA 901-9984 001-01SA
CS515 2 121314 4 45 2 000-12SA 001-01SA
CH302 1 12116 5 15 3 000-12SA 901-9984 001-01SA
MA111 1 12333 4 15 1 000-12SA
PH999 1 12999 2 10 0000-12SA901-9984
PY000 3 12332 6 5 2 000-45SA 901-9984
bash-4.2$ ./a.out students.dat courses.dat
begin
objects created
+++ Before the if statement +++
+++ 548 +++
+++ After +++
+++ Before the if statement +++
+++ 626 +++
+++ After +++
+++ Before the if statement +++
+++ 605 +++
+++ After +++
+++ Before the if statement +++
+++ 915 +++
Segmentation fault
gdb输出:
Starting program: /home/csu/dtk24/cs515/prog11/a.out students.dat courses.dat
begin
objects created
+++ Before the if statement +++
+++ 548 +++
Program received signal SIGSEGV, Segmentation fault.
0x0804b2a0 in studentsDB::push (this=0xbffff78c, std=0x8054120)
at studentsDB.cpp:130
130 if( sDB[index] == 0 ) {
答案 0 :(得分:0)
您的问题中没有足够的代码可以确定问题是什么。
然而,如果我冒险猜测,那么问题很可能是在hash()中 - 这取决于你如何进行计算最终得到一个负数,或者一个大于哈希表的最大值的数< / p>
答案 1 :(得分:0)
你写了一个双“;”字符。 错误是在if。之前的行引起的。