2012-04-22 152 views
1

我編譯一次文件,並使用相同的輸出運行它,並且我僅在大約20-30%的時間內出現分段錯誤。我只是給我的教授發了郵件,但她永遠都在。我真的不知道爲什麼,特別是因爲錯誤不會一直髮生,但有時只有完全相同的a.out文件。我知道錯誤發生的位置,來自if(sDB [index] == 0)。隨機分割故障C++

感謝您的幫助,

請讓我知道如果需要更多的代碼來解決這個問題。

* 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) { 
+2

用valgrind運行它。 – 2012-04-22 23:45:46

+3

你怎麼知道'std-> hash()'返回的散列值對'sDB'(即'MAX')的範圍有效? – 2012-04-22 23:46:17

+1

散列函數的可能輸出是什麼?你有沒有檢查是否每次都分配了sDB?你可以發佈std-> hahs()的代碼嗎?前面的 – 2012-04-22 23:48:12

回答

0

沒有包含在你的問題可以肯定地說是什麼問題,不充分的代碼。

但是,如果我想冒險一個猜測,那麼問題可能是在哈希() - 這取決於你如何做計算結束了一個負數或大於哈希表的最大值

0

你寫了一個雙「;」字符。 錯誤是在if之前的行處引起的。

+0

看看上面的評論,問題已經被發現(2012年4月)。這個問題從來都不是空洞的陳述(即「某事;;」)。 – Hasturkun 2013-02-24 10:48:22