Я скомпилирую файл один раз и запускаю его с тем же выходом, и я получаю ошибку сегментации только около 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) {
Запустите его с valgrind. –
Как вы знаете, что значение хэша, возвращаемое 'std-> hash()', допустимо для диапазона 'sDB' (который является' MAX')? –
Каковы возможные выходы хэш-функции? Вы проверяли, был ли sDB выделен каждый раз? Можете ли вы опубликовать код для std-> hahs()? –