我得到一個「分段故障(核心轉儲)」運行時錯誤與下面的代碼:段錯誤C++
#include <iostream>
#include "Student.h"
#include "SortedList.h"
using namespace std;
#define BOUNDS 100
int main() {
SortedList *list = new SortedList(); // points to the sorted list object
Student *create[BOUNDS]; // array to hold 100 student objects
int num = 100000; // holds different ID numbers
// fills an array with 100 students of various ID numbers
for (int i = 0; i < BOUNDS; i++) {
create[i] = new Student(num);
num += 10;
}
// insert all students into the sorted list
for (int i = 0; i < BOUNDS; i++)
list->insert(create[i]);
// individually deletes each student
num = 100000;
for (int i = 0; i < BOUNDS; i++) {
delete list->find(num);
num += 10;
}
// insert all students into the sorted list
for (int i = 0; i < BOUNDS; i++)
list->insert(create[i]);
num = 100000;
for (int i = 0; i < BOUNDS; i++) {
list->remove(num);
num += 10;
}
cout << "test2" << endl;
delete list;
return 0;
}
我已經縮小誤差下降到delete list;
線(或哪一個至上)。我只是想知道這是爲什麼,以及如何解決它。任何關於這個問題的見解都是有用的。
你似乎在unixoid系統上運行這個。所以使用Valgrind;) – 0xC0000022L 2012-04-11 23:29:16
「//填充100名具有各種ID號的學生的數組」這個循環不會做這個評論所說的話。 – 2012-04-11 23:29:23
作爲參考,每次有人用'namespace std;'來命名',我就踢一隻小狗。在再次做這件事之前想想可憐的小狗。 – cHao 2012-04-11 23:31:35