0

I have been writing a linked list implementation of a set of integers ADT and have been getting a double free or corruption error when I compile and run my code, and for the life of me I can't figure out the source. I thought originally it was in the destructor but that does not appear to be the issue after I tinkered with it a decent amount. I'm attaching my code for review.

class list_intset: public intset {

    struct node {
        int number;
        node *next;
        node(int n, node * n2 = NULL):number(n),next(n2){}
    };
    mutable node * head;
    int set_size;

    list_intset(const list_intset & a) { //copy constructor thing
        throw std::domain_error("No copy constructor ");
    }
    list_intset & operator =(const list_intset & a){ //= sign operator
        throw std::domain_error("No = for intset");
    }
//  
public:
    list_intset(){ //default constructor that constructs the empty set
        set_size = 0;
        head = NULL;
    }; 
    virtual ~list_intset(){ //virtual devastator
        node * temp;
        while(head != NULL){
            temp = head->next;
            delete head;
            head = temp;
        }
    }; 

//  //basic operations
    void add(int a){ //add an item to the set
        head = new node(a, head);
        ++set_size;
    }
//  
    void remove(int a){ //removes an item from the set //ask professor about this one, I'm not so sure
        node * temp;
        node * ptr = head;
        while(ptr && ptr->number != a){
            ptr = ptr->next;
        }
        if(ptr != NULL){
            temp = ptr;
            ptr = ptr->next;
            cout << "deleting temp in remove."<<endl;
            delete temp;
        }
    }
    void do_clear(node * n){
        if(n != NULL){
            do_clear(n->next);
            cout << "deleting n in do_clear."<<endl;
            delete n;
        }
    }
    void clear(){ //removes all elements from the set
        do_clear(head);
        set_size = 0;
    }
//  
    bool contains(int a) const{ //returns true if an element is in the set, returns false if it is not.
        node * temp_head = head;
        bool flag = false;
        while(temp_head != NULL){

            if(temp_head->number == a){
                flag = true;
                break;
            }else{
                temp_head = temp_head->next;
            }
        }
        return flag;
    }
    int size() const{ //returns how many elements are in the set
        cout << "size" << endl;
        return set_size;
    }
    void display(std::ostream & a) const{ //displays all of the elements in a set with the curly brackets, ex: {2 3 4 7 9}
        node * temp = head;
        cout << "display" << endl;
        a << "{";
        for(int i = 0; i < set_size; i++){
            a << temp->number << " ";
            temp = temp->next;
        }
        a << "}";
    }

I left out the set operation functions I had written as I was not doing any deleting in those functions. Also, the cout statements such as "deleting temp in remove" I was using to try and track down the errors during running and compiling.

Thanks, David

  • 2
    A [mcve] is meant to be minimal. Producing an MCVE is a skill you will need throughout your career as a developer. Better make sure that you can produce an MCVE. – IInspectable Feb 09 '17 at 20:01
  • *I left out the set operation functions I had written as I was not doing any deleting in those functions.* -- But you're doing something in those functions that may down the road have caused the error. This is why we need a [mcve], complete with how you're using your class in a small `main` function that reproduces the error. – PaulMcKenzie Feb 09 '17 at 20:10

0 Answers0