2012-04-12 135 views
1
#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]); 

    // removes each student from the list 
    num = 100000; 
    for (int i = 0; i < BOUNDS; i++) { 
    list->remove(num); 
    num += 10; 
    } 

    delete list; 
    return 0; 
} 

我得到一個seg錯誤與以前的代碼。任何有關爲什麼這是或如何可能解決它的見解將不勝感激。賽格故障肯定是由delete list;線造成分段錯誤C++

更新1:這是我的排序列表析構函數

/* 
* Destructs this sorted list object 
*/ 
SortedList::~SortedList() { 
    freeList(head); 
} 

/* 
* Traverses throught the linked list and deallocates each node 
*/ 
void SortedList::freeList(Listnode *L) { 
    Listnode *tmp = L; //holds the node to be deleted 

    //traverses the list 
    while (tmp != NULL) { 
     Listnode *next = tmp->next; //holds the value of the next node 

    //delete previous node 
    delete tmp->student; 
    delete tmp->next; 
    delete tmp; 

    //sets the next node to the node to be deleted 
    tmp = next; 
    } 
    //delete header node 
    delete L; 
} 
+0

'delete list'應該調用'〜SortedList()',那麼析構函數做什麼都不平凡? – 2012-04-12 02:42:23

+0

我會在一分鐘後發佈SortedList析構函數,如果你關心它的話。 – 2012-04-12 02:44:21

+0

所以一個修正是從'SortedList :: freeList'中刪除'delete tmp-> next;'行。是否SortedList擁有所有交給它的存儲?如果它不擁有'student'指向的空間,那麼'delete tmp-> student;'可能不正確。目前還不清楚SortedList :: remove的作用。這會刪除ListNode,還是刪除ListNode並刪除'student'成員?如果它也刪除'student'成員,那麼它將與'SortedList :: freeList'一致,否則它不會。 – gbulmer 2012-04-12 03:44:33

回答

2

freelist(),您刪除tmp->next,然後設置tmp = tmp->next。現在tmp有一個無效的指針。您需要重構代碼,以便在訪問其成員之前不釋放指針。

雖然我討厭做人民的功課對於他們來說,這裏是我的解決方案:

/* 
* Traverses throught the linked list and deallocates each node 
*/ 
void SortedList::freeList(Listnode *L) { 
    if(L == NULL) return; 
    freeList(L->next); 
    delete L->student; 
    delete L; 
} 

這種使用O(N)爲刪除堆棧空間,但我個人覺得比一個循環清晰。您可以通過刪除對delete tmp->next的呼叫來調整您的解決方案以「正常工作」。

+0

+1簡單的freeList()程序。讀完最後一條評論後,我會刪除我的答案。 – karlphillip 2012-04-12 03:17:25

+0

儘管有效,但它爲可迭代遍歷的數據結構燒了很多堆棧空間。 SortedList :: freeList(Listnode * L)中的錯誤僅在兩次刪除相同的空間。 – gbulmer 2012-04-12 03:17:29

4

好了,我們無法看到SortedListStudent,而且我想這個問題是在一個那些。我注意到num在創建循環之後永遠不會重置爲其原始值,這意味着大多數remove調用將被傳遞一個id,該編號屬於no Student;也許那個案子失敗了。或者,對於這個問題,在insertremove方法或者構造函數或析構函數中可能只有一些錯誤。它完全在空中。

編輯:正如其他人指出的,該析構函數刪除後使用指針;這可能是唯一的錯誤來源,或者我們還沒有看到的代碼中可能會有更多的錯誤。

+0

OP已更新他的問題。 – karlphillip 2012-04-12 03:07:36

1
// removes each student from the list 
    for (int i = 0; i < BOUNDS; i++) { 
    list->remove(num); 
    num += 10; 
    } 

看起來有趣...這是如何工作的?如果在代碼中的這一點上num是100000 + BOUNDS * 10(因爲它爲你創建的每個學生添加10後永遠不會改變)。每次刪除您撥打的電話都不會刪除學生的ID(因爲所調用的ID是100000 + BOUNDS * 10 + i * 10)。是否意圖通過ID刪除它們,如果是這樣的話,您應該考慮在執行刪除循環之前將num重置爲100000。

澄清如何這可能導致seg-fault:如果您的刪除功能沒有適當的邊界檢查它可能會出內存查找id刪除。

更新與析構函數的問題:

void SortedList::freeList(Listnode *L) { 
    Listnode *tmp = L; //holds the node to be deleted 

    //traverses the list 
    while (tmp != NULL) { 
     Listnode *next = tmp->next; //holds the value of the next node 

    //delete previous node 
    delete tmp->student; 
    delete tmp->next; 
    delete tmp; 

    //sets the next node to the node to be deleted 
    //********** 
    //Check here, you deleted next, but the assigned it to temp. Tmp isn't null, but   
    //it is however, no longer your memory (since you deleted it) 
    //********** 
    tmp = next; 
    } 
    //delete header node 
    delete L; 
} 
+0

對不起,我糾正了 – 2012-04-12 02:49:04