C++ 访问链表时出现分段错误11

C++ 访问链表时出现分段错误11,c++,linked-list,segmentation-fault,C++,Linked List,Segmentation Fault,我肯定我把我的指针弄错了,或者可能是开头的NULL,但我想不出来 我正在尝试将链接列表写入文本文件: write_out(node *ll){ ofstream out; out.open("output.txt"); if (!out.is_open()) exit(EXIT_FAILURE); cout << ll->value; //stuff to write out } 但是行cout value导致分段错误:11,我不明

我肯定我把我的指针弄错了,或者可能是开头的
NULL
,但我想不出来

我正在尝试将链接列表写入文本文件:

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

    cout << ll->value;

    //stuff to write out
}
但是行
cout value
导致
分段错误:11
,我不明白为什么

我已经注释掉了我实际上要写的代码,因为这是不相关的,问题显然是我(缺乏)理解上面的工作原理

我调用
write_out(linkedlist)
其中
node*linkedlist
指向第一个节点

这种情况发生在:

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
}

这肯定没有留下
ll=NULL

我可以猜问题出在向列表添加新节点的函数中

我想你也会做类似的事情

void add_node( node *n, int value );

node *linkedlist = NULL;

add_node( linkedlist, somevalue );
在这种情况下,函数中linkedlist的任何更改都不会影响原始对象linkedlist。所以它仍然等于NULL。因此,当您尝试输出列表并使用

cout << ll->value;
cout值;
ll等于NULL

read_in(node *ll){

ll
是按值传递的参数。这意味着在
read_in
中对其进行的任何更改都只是它的局部更改,在它之外没有任何影响。因此,在完成
read_in
之后,指向列表头部的指针仍然是
NULL
(假设您使用该指针初始化指针)。因此,使用
NULL
参数调用
write\u out
,将取消对空指针的引用,这将导致您的SIGSEGV。

这只是一个简单的示例,添加了@Michael Foukarakis指出的内容

#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;
}
#包括
无效此\u不更改\u ptr(int*a,int val){
a=新整数;
*a=val;
}
作废此更改本身(int**a,int val){
*a=新整数;
*(*a)=val;
}
int main(){
int*my_ptr=NULL;
这个“不改变”的ptr(我的ptr,5);
if(my_ptr==NULL){

std::你能在调用
write\u out
的地方显示代码吗?听起来好像
linkedlist
是一个空指针或无效指针。呃,比听起来更难显示。我会执行
node*linkedlist=null
,然后
读入
,然后
写出
我会在上面发布
read\u in
,以防它被保留为ode>NULL
@JohnKugelman-已添加。除其他答案外,我稍微担心链接列表中的最后一个节点有一个未定义的下一个指针,该指针应显式为NULL。感谢@sharth,将修复此问题。出于某种原因,(n-1)th
next
是空的,而不是指向第n个..你能明白为什么吗?啊!这可能是-你能从我提供的
read\u中验证一下吗?@Ollie Ford你应该正确地编写函数。它必须重新生成修改后的linkedlist指针,或者相应的参数必须定义为指向节点的指针,或者指向节点指针的引用。我最初试图通过引用传递
read_-in(node&ll){
,但我不知道如何处理
ll
,编译器不会对此抱怨。我是否可以返回
ll
的新值,并在调用时分配它:
ll=read_-in(ll)
?谢谢。我现在也有同样的问题(至少现在我知道它是什么!),因为(n-1)th没有指向n。所有其他人都写得很好,但我不明白为什么?@sharth在评论中给出了一个关于这一点的指针,请查看。我没有看到它,它被删除了吗?是的。
#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;
}