Ashkan Kazemi
Ashkan Kazemi

Reputation: 1097

linked list segmentation fault fixed with cerr

I have the following code . what this code does is that it merges two sorted linked lists and then deletes both the linked lists and returns a new list :

#include <iostream>

using namespace std;

class node {
    public:
        node(int, node*);
        node* get_next() const;
        void set_next(node*);
        int get_item() const;
        void set_item(int);
    private:
        int item;
        node* next;
};

node* clone_and_destroy(node* a)
{
    node* new_list;
    node* prev;
    node* ret_val;
    while ( a )
    {
        new_list = new node(a->get_item(),NULL);
        if ( prev ){
            prev->set_next(new_list);
            prev = prev->get_next();
        }
        else{
        prev = new_list;
        ret_val = prev;
        }
        node* temp = a;
        a = a->get_next();
        delete temp;
    }
    return ret_val;
}

node* merge(node*& a, node*& b){
    if ( !a ){// cerr<<"\r";
        return clone_and_destroy(b);}
    if ( !b )
        return clone_and_destroy(a);

    node* smaller_node, *bigger_node;
    if ( a->get_item() < b->get_item() ){
        smaller_node = a;
        bigger_node = b;
    }
    else {
        smaller_node = b;
        bigger_node = a;
    }
    node* next_node = smaller_node->get_next(); 
    node* merged_list = new node(smaller_node->get_item(),smaller_node->get_next());
    delete smaller_node;
    merged_list->set_next(merge(next_node,bigger_node));
    a = NULL ;
    b = NULL ;
    return merged_list;
}

/*node* merge(node*& a, node*& b){
    if ( !a )
        return b;
    if ( !b )
        return a;
    if ( a->get_item() < b->get_item() ){
        node* a_next = a->get_next();
        node* merged_list = a;
        merged_list->set_next(merge(a_next,b));
        delete a;
        return merged_list;
    }
    else {
        node* b_next = b->get_next();
        node* merged_list = b;
        merged_list->set_next(merge(a,b_next));
        delete b;
        return merged_list;
    }
}*/     

void print(node* a)
{   
    while ( a )
    {
        cout << a->get_item() << " ";
        a = a->get_next();
    }
    cout << endl;
}

node::node(int _item, node* _next)
{
    item = _item;
    next = _next;
}
node* node::get_next() const
{
    return next;
}
void node::set_next(node* new_next)
{
    next = new_next;
}
int node::get_item() const { return item; }
void node::set_item(int _item)
{ 
    item = _item;
}

int main() {
    node* l1 = NULL, *l2 = NULL;
    cout << "Before Merging: " << endl;
    for (int i = 5; i > 0; i--) {
        l1 = new node(i, l1);
        l2 = new node(2*i-3, l2);
    }
    cout << "List 1 is: \t\t";
    print(l1);
    cout << endl;
    cout << "List 2 is: \t\t";
    print(l2);
    cout << endl << "After Merging:" << endl;
    node* m = merge(l1, l2);
    cout << "List 1 is: \t\t";
    print(l1);
    cout << "Should be: \t\t[ ]" << endl;
    cout << endl;
    cout << "List 2 is: \t\t";
    print(l2);
    cout << "Should be: \t\t[ ]" << endl;
    cout << endl;
    cout << "Merged List is: \t";
    print(m);
    cout << "Should be: \t\t[ -1 1 1 2 3 3 4 5 5 7 ]" << endl;


    for (node* h = m; h != NULL; h = m) {
        m = m->get_next();
        delete h;
    }
    return 0;
}

the important part of the code is the merge function , the rest are just tools for implementing this function . now a funny thing happens ! this code gets a segmentation fault when I run it , but when I was trying to debug my code I used cerr on the first line of the merge function ( it's commented ) and suddenly it worked fine !!! can someone please explain this to me !? what is this cerr doing ?! and how can I fix my code without this cerr and with minimum changes to the code !? now I know that cerr stops the couts from buffering but I dont think thats the case here !

Upvotes: 0

Views: 251

Answers (1)

Digital_Reality
Digital_Reality

Reputation: 4738

*prev is not initialized with anything and you are trying to access it. Hence seg. fault.

node* clone_and_destroy(node* a) { .. ..

     if ( prev ){

Edit: Initializing it will null fixed the seg fault. but you check logic behind it.

node* prev = NULL;

Edit2: Why cerr fixes segmentation fault

Code crashes unless I put a printf statement in it

Abstract from it "The question asks "why does the printf() statement 'fix' things". The answer is because it moves the problem around. You've got a Heisenbug because there is abuse of the allocated memory, and the presence of the printf() manages to alter the behaviour of the code slightly."

Upvotes: 5

Related Questions