简体   繁体   English

分段错误C ++

[英]Segmentation Fault 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]);

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

    delete list;
    return 0;
}

I am getting a seg fault with the previous code. 我之前的代码出现段错误。 Any insight as to why this is or how to possibly fix it would be appreciated. 任何关于这是为什么或如何解决它的见解将不胜感激。 The seg fault is definitely caused by the delete list; 段错误肯定是由delete list;引起的delete list; line 线

UPDATE 1: Here is my SortedList destructor 更新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;
}

Well, we can't see SortedList or Student , and I'd guess the problem is in one of those. 好吧,我们看不到SortedListStudent ,我猜问题出在其中之一。 I note that num never gets reset to its original value after the creation loop, which means that most of the remove calls are going to be passed an id that belongs to no Student ; 我注意到num在创建循环之后再也不会重置为其原始值,这意味着大多数remove调用都将传递一个不属于Student的id; perhaps that case fails. 也许那个案例失败了。 Or perhaps there are simply bugs in the insert or remove methods -- or the constructor or destructor, for that matter. 或者,在此方法中,在insertremove方法中可能仅存在错误-或构造函数或析构函数。 It's totally up in the air. 它完全在空中。

EDIT: As others have pointed out, that destructor uses a pointer after it's been deleted; 编辑:正如其他人所指出的,该析构函数在删除后使用指针; that could be the only source of error, or there could easily be more in the code we haven't seen yet. 那可能是唯一的错误来源,或者很容易在代码中出现我们还没有看到的更多错误。

In freelist() , you delete tmp->next , then set tmp = tmp->next . freelist() ,删除tmp->next ,然后设置tmp = tmp->next Now tmp has an invalid pointer. 现在, tmp具有无效的指针。 You need to restructure your code so that you do not free a pointer before accessing its members. 您需要重组代码,以便在访问其成员之前不释放指针。

Although I hate doing people's homework for them, here's my solution: 尽管我讨厌为他们做家庭作业,但这是我的解决方案:

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

This use O(n) stack space for deletion, but I personally find it much clearer than a loop. 这使用O(n)堆栈空间进行删除,但我个人认为它比循环更清晰。 Your solution can be tweaked to "just work" by removing the call to delete tmp->next . 您可以通过删除delete tmp->next的调用来delete tmp->next您的解决方案调整为“正常工作”。

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

Looks interesting... how does this work exactly? 看起来很有趣……这是如何工作的? If num is 100000 + BOUNDS*10 at this point in the code (since it is never changed after you add 10 to it for each student you create). 如果此时代码中的num是100000 + BOUNDS * 10(因为在为您创建的每个学生添加10后就永远不会更改)。 Every remove call you make doesn't remove a student by their ID (since the id called is 100000 + BOUNDS*10 + i*10). 您拨打的每个删除电话都不会删除其ID的学生(因为该ID是100000 + BOUNDS * 10 + i * 10)。 Was the intent to remove them by ID, if so you should consider resetting num to 100000 before doing the remove loop. 目的是通过ID删除它们,如果这样,则应在执行remove循环之前考虑将num重置为100000。

To clarify how this could cause the seg-fault: if your remove function doesn't have proper bounds checking it could go out of the memory looking for the id to remove. 为了弄清楚这是如何导致段错误的:如果您的remove函数没有正确的边界检查,它可能会从内存中消失以寻找要删除的id。

Updated with destructor question: 更新了析构函数问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM