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;
}

我在前面的代码中遇到了段错误。任何关于为什么会这样或如何解决它的见解将不胜感激。delete list;seg故障肯定是线路引起的

更新 1:这是我的 SortedList 析构函数

/*
 * 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;
}
4

3 回答 3

4

好吧,我们看不到SortedListor Student,我猜问题出在其中之一。我注意到num在创建循环之后永远不会重置为其原始值,这意味着大多数remove调用将被传递一个属于 no 的 id Student;也许那个案子会失败。或者可能只是在insertorremove方法中存在错误 - 或者构造函数或析构函数,就此而言。它完全悬而未决。

编辑:正如其他人指出的那样,析构函数在删除后使用指针;这可能是唯一的错误来源,或者我们还没有看到的代码中很容易出现更多错误。

于 2012-04-12T02:42:21.247 回答
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.

于 2012-04-12T02:54:55.237 回答
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。

澄清这如何导致段错误:如果您的删除函数没有适当的边界检查,它可能会超出内存寻找要删除的 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;
}
于 2012-04-12T02:47:37.607 回答