【问题标题】:linkedlist: count=count->next gives segmentation fault链表:count=count->next 给出分段错误
【发布时间】:2018-05-07 16:39:33
【问题描述】:
#include<stdio.h>
#include<malloc.h>
typedef struct nde{
    int data;
    struct nde *next;
}node,*pnode;
void inst_beg(node *,int);
void inst_end(node *,int);
void inst_any(node *,int,int);
int del_begin(node *);
int del_end(node *);
int del_any(node*,int);
void display(node *);
main()
{
    pnode head= (node *)malloc(1*sizeof(node));
    head->data=0;
    head->next=NULL;
    inst_any(head,1,1);
    inst_any(head,2,2);
//  inst_any(head,3,3);
    display(head);
}

void inst_any(node *head,int pos, int data){
    pnode nd=(node *)malloc(1*sizeof(node));
    nd->data=data;
    //pnode count=(node *)malloc(1*sizeof(node));
    pnode count;
    count=head;
    printf("head: %p",head);
    printf("count: %p",count);
    int i=0;
    while(i<pos-1){
        count=count->next; //Problem is here for inst_any(phead,2,2) 
    }
    nd->next=count->next;
    count->next=nd;
    //printf("done");   
}

void display(node * head){
    pnode count=head;
    while(count->next!=NULL){
        printf("%d",count->data);
        count=count->next;
    }
}

count 的值在循环内变为 null,因此当第二次调用 inst_any(head,2,2) 时,我们无法尊重它。使用 gdb 检查第一次计数是否成功指向头部。同样的情况也第二次发生。在 count=head 之后,它第二次给出正确的值。不知道之后发生了什么。为什么当它进入循环计数的值变为零时。

【问题讨论】:

  • 除了我的回答,为避免以后发生此类事情,请阅读How to debug small programs。 “橡皮鸭调试”会话应该已经解决了这个错误。
  • 嗨,Subhabrata,请检查下面我的答案并更正您的代码。还请阅读以下代码中的所有 cmets。
  • 该函数有两个有效签名:main() 1) int main( void ) 和 2) int main( int argc, char *argv[] ) I.E.发布的代码使用了无效的签名。
  • 在调用任何堆分配函数时:(malloc, calloc, realloc) 1) 始终检查 (!=NULL) 返回值以确保操作成功。 2)返回的类型是void*,可以分配给任何指针。强制转换只会使代码混乱,使其更难以理解、调试等。注意:将任何大小乘以 1 绝对没有效果。
  • @user3629249 实现定义的main() 签名也是“有效的”,但当然不能移植。

标签: c pointers linked-list segmentation-fault


【解决方案1】:

看看这段代码:

while(i<pos-1){
    count=count->next; //Problem is here for inst_any(phead,2,2) 
}

这就是整个循环。因此,要么您的条件 i&lt;pos-1 立即为 false .... 或者它为 true 并且 保持 true,因为您从未在循环中修改 ipos

在后一种情况下,你遍历一个链表。最终,你会找到结尾(count-&gt;nextNULL)并且仍然将这个NULL 分配给count。在下一次迭代中,您尝试取消引用 NULL 以访问 -&gt;next。尝试取消引用 NULL未定义的行为,典型的后果是分段错误。

去重新考虑你的程序(例如,检查循环条件中的count-&gt;next 是否仍然不是NULL)。

【讨论】:

    【解决方案2】:

    我已经用下面的 cmets 更正了您的代码。请阅读我的代码中的 cmets,否则您将无法意识到自己的错误。希望这会对你有所帮助。

    typedef struct nde{
        int data;
        struct nde *next;
    }node,*pnode;
    
    void inst_beg(node *,int);
    void inst_end(node *,int);
    void inst_any(node *,int,int);
    int del_begin(node *);
    int del_end(node *);
    int del_any(node*,int);
    void display(node *);
    
    
    void main()
    {
        pnode head= (node *)malloc(sizeof(node)); //No need to multiply by one
        head->data=0;
        head->next=NULL;
        inst_any(head,1,1);
        inst_any(head,2,2);
        inst_any(head,3,3);
        display(head);
        inst_any(head,4,4); //I am adding this statement so that you can better understand where it going to be inserted
        display(head);
        inst_any(head,7,7);
    }
    
    void inst_any(node *head,int pos, int data){
        pnode nd=(node *)malloc(sizeof(node));
        nd->data=data;
        //pnode count=(node *)malloc(1*sizeof(node));
        pnode count;
        count=head;
        printf("head: %p\n",head);
        printf("count: %p\n",count);
        int i=0;
        while(i < (pos-1)){
            if(count == NULL){
             printf("No position available for request pos =%d\n", pos);
             return;//This condition is important. If your position is not exist in the list and count reached the end of the list just return with a error message
           }
            count=count->next; //Problem is here for inst_any(phead,2,2)
            i++;//you must increment i
        }
        nd->next=count->next;
        count->next=nd;//Here count must not be null, else it will create Segmentation fault. Therefore inside from while loop above we have checked whether it is null or not. If null return from this method. 
        //printf("done\n");
    }
    
    void display(node * head){
        pnode count=head;
        while(count!=NULL){//You have to correct it to print last node of the list
            printf("%d\n",count->data);
            count=count->next;
        }
    }
    

    【讨论】:

      【解决方案3】:

      函数的贴出代码:inst_any() 初始化data 字段,但未能初始化next 字段。

      建议:

      nd->data=data;
      nd->next = NULL;
      

      然后这个循环:

      while(i<pos-1){
          count=count->next; //Problem is here for inst_any(phead,2,2)
      }
      

      无法更新计数器i,因此循环永远不会退出。此外,当链表不包含足够的条目时,此循环将在链表末尾运行。所以循环还需要检查count-&gt;next 不为NULL。

      【讨论】:

        猜你喜欢
        • 2021-07-10
        • 1970-01-01
        • 2014-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-08
        • 2015-06-01
        相关资源
        最近更新 更多