0

I'm sure I've screwed up my pointers, or perhaps the initial NULL, but I can't figure it out.

I'm trying to write a linked list out to a text file:

write_out(node *ll){
    ofstream out;
    out.open("output.txt");
    if (!out.is_open()) exit(EXIT_FAILURE);

    cout << ll->value;

    //stuff to write out
}

and:

struct node {
    int value;
    node *next;
}

But the line cout << ll->value causes Segmentation fault: 11, I do not understand why however.

I've commented out the code I was actually doing to write out, as this is irrelevant, the issue is obviously with my (lack) of understanding how the above works.

I call write_out(linkedlist) where node* linkedlist points to the first node.

This happens after:

read_in(node *ll){
    ifstream data; //opened and checked open as above for out
    int v;
    ll = new node;
    node *tmp = ll;
    data >> tmp->value;
    while(data >> v){
        tmp->next = new node;
        tmp = tmp->next;
        tmp->value = v;
    }
    tmp->next = NULL;  //thanks @sharth
}

Which surely hasn't left ll = NULL?

4
  • 2
    Can you show the code where you call write_out? It sounds like linkedlist is a null or invalid pointer. Commented Feb 11, 2014 at 19:47
  • Erm, tougher than it sounds to show. I do node* linkedlist = NULL, then read_in, and then write_out. I'll post read_in above, in case that is leaving it as NULL. Commented Feb 11, 2014 at 19:53
  • 2
    Additionally to the other answers, I'm slightly concerned that the last node in your linked list has an undefined next pointer, when it should be explicitly null. Commented Feb 11, 2014 at 20:12
  • Thanks @sharth, will fix. For some reason, the (n-1)th next is null rather than pointing to the nth.. can you see why? Commented Feb 11, 2014 at 20:27

3 Answers 3

2
read_in(node *ll){

ll is a parameter passed by value. That means any changes to it inside read_in are only local to it and have no effect outside it. Therefore after read_in is done, the pointer to the head of your list is still NULL (assuming that's what you initialized the pointer with). Calling write_out with a NULL parameter therefore dereferences a NULL pointer, which will cause your SIGSEGV.

Sign up to request clarification or add additional context in comments.

6 Comments

I initially tried to pass by reference read_in(node &ll){, but I couldn't figure out how to then do anything with ll that the compiler wouldn't complain about. Can I return the new value of ll instead, and assign it when called: ll = read_in(ll)?
Thanks. I have the same issue (at least now I know what it is!) now in that the (n-1)th does not point to the nth. All the others write out fine, but I can't see why?
@sharth has given a pointer about that in the comments, check it out.
I'm not seeing it, has it been deleted?
Here it is.
|
0

I can guess that the problem is in the function where you add new nodes to the list.

I think you do something similar

void add_node( node *n, int value );

node *linkedlist = NULL;

add_node( linkedlist, somevalue );

In this case any changes of linkedlist inside the function does not influence the original object linkedlist. So it will still be equal to NULL. So when you try to output the list and use

cout << ll->value;

ll is equal to NULL.

2 Comments

Ah! This could be it - can you verify from my read_in as supplied?
@Ollie Ford You should write the function correctly. Either it has to retrurn the modified linkedlist pointer or the corresponding parameter has to be defined as pointer to pointer to node or reference to pointer to node.
0

Just a simple example to added what @Michael Foukarakis pointed out

#include<iostream>

void this_dont_change_ptr(int* a, int val){
    a = new int;
    *a = val;   
}

void this_changes_ptr_itself(int** a, int val){
    *a = new int;
    *(*a) = val; 
}

int main(){

    int *my_ptr = NULL;
    this_dont_change_ptr(my_ptr, 5);

    if(my_ptr == NULL){
        std::cout << "In fact, ptr is still NULL" << std::endl;
    }

    // What I do with allocated memo??

    // grants that my_ptr is NULL again
    my_ptr = NULL;  
    this_changes_ptr_itself(&my_ptr, 5);
    if(my_ptr == NULL){
        std::cout << "MUST never get here!" << std::endl;
    }
    else{
        std::cout << "Now we have a new value " << *my_ptr << std::endl;    
    }

    delete my_ptr;  

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.