0

我编译了一次文件,并以相同的输出运行它,但我只有大约 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 ) {
4

2 回答 2

0

你写了一个双“;” 特点。错误是在 if 之前的行引起的。

于 2013-02-24T10:42:11.373 回答
0

您的问题中包含的代码不足,无法确定问题所在。

但是,如果我冒昧地猜测一下,那么问题很可能出在 hash() 中——这取决于你如何进行计算,最终得到一个负数,或者一个大于哈希表最大值的数字

于 2012-04-22T23:49:21.457 回答