Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7792013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:04:02+00:00 2026-06-01T22:04:02+00:00

#include <iostream> #include Student.h #include SortedList.h using namespace std; #define BOUNDS 100 int main()

  • 0
#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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:04:03+00:00Added an answer on June 1, 2026 at 10:04 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { double u = 0; double w
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> #include <stack> #include <string> using namespace std; int main() { string name;
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include iostream #include vector using namespace std; const vector<int>& Getv() { vector<int> w(10); w[0]=10;
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <iostream> using namespace std; // This first class contains a vector and a
I have the following code: #include stdafx.h #include <iostream> #include <conio.h> using namespace std;
I have the following code: #include <iostream> #include Student.h <-- fixed the problem #include
#include iostream class A { private: int a; public : A(): a(-1) {} int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.