简体   繁体   中英

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

UPDATE 1: Here is my SortedList destructor

/*
 * 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. 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 ; perhaps that case fails. Or perhaps there are simply bugs in the insert or remove methods -- or the constructor or destructor, for that matter. 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 . Now tmp has an invalid pointer. 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. Your solution can be tweaked to "just work" by removing the call to 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). Every remove call you make doesn't remove a student by their ID (since the id called is 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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