0

ファイルを 1 回コンパイルし、同じ出力で実行すると、約 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 に答える