c - C 中的段错误(核心转储)

标签 c data-structures linked-list segmentation-fault coredump

我的代码中出现段错误,它会在单链表中插入数据,然后打印相同的数据。我完全不知道它是从哪里来的。这是我的代码。任何形式的建议或意见都是非常受欢迎的。谢谢。

#include<stdio.h>
#include<stdlib.h>
void insert_data(int data);
void print_list();
typedef struct node{
    int info;
    struct node* next;
    }mynode; 

mynode *head=NULL,*tail=NULL,*ptr = NULL;
int main()
{
        int nodes,i;
    int data;
    printf("how many nodes u want to insert");
    scanf("%d",&nodes);
    for(i=0;i<nodes;i++)
    {
        printf("Enter data");
        scanf("%d",&data);
        insert_data(data);
    }
    print_list();
    return 0;
}
void insert_data(int data)
{
    ptr = (mynode *)malloc(sizeof(mynode));
    (*ptr).info = data;
    if(head == NULL)
    {
    (*ptr).next = NULL;
    head = ptr;
    tail = ptr;
    }
    else
    {
        printf("inside else");
        (*ptr).next=NULL;
        (*tail).next = ptr;
        tail = ptr;
    }
    return;
}
void print_list()
{   
    ptr = head;
    while((*ptr).next != NULL)
    {
        printf("%d",(*ptr).info);
        ptr = (*ptr).next;
    }   
    printf("%d",(*ptr).info);
}

最佳答案

if(head=NULL) 应该是 if(head==NULL)

此外,通常不鼓励使用太多全局变量。您可以在每个函数中将 ptr 声明为局部变量。这样做可以防止函数之间出现奇怪的交互。

例如:

void print_list()
{   
  mynode* ptr2 = head; // ptr2 is local : it only exist in this function.
  while((*ptr2).next != NULL)
  {
    printf("%d",(*ptr2).info);
    ptr2 = (*ptr2).next;
  }   
  printf("%d",(*ptr2).info);
}

如果列表为空,上面的函数就会失败:在 head 上添加测试可能是一件好事!

最后,可以修改该函数以处理任何列表 mynode*,而不仅仅是 head :

void print_list(mynode* somelisthead)
{   
  if(somelisthead==NULL){printf("empty list\n");return;}
  mynode* ptr2 = somelisthead; // ptr2 is local : it only exist in this function.
  while((*ptr2).next != NULL)
  {
    printf("%d ",(*ptr2).info);
    ptr2 = (*ptr2).next;
  }   
  printf("%d",(*ptr2).info);
}

现在在 main 中调用为 print_list(head);

您发布的代码调用 malloc() :这为每个节点分配内存。我猜想接下来的步骤是使用 free() 编写一个函数来释放内存!

以下代码是否仍然存在段错误?

#include<stdio.h>
#include<stdlib.h>


typedef struct node{
    int info;
    struct node* next;
}mynode; 

void print_list(mynode* somelisthead);
// why ** ? because head and tail are modified by the function. Therefore, passing by argument is required, and a pointer to head named phead is provided to the function.
void insert_data(int data, mynode** phead,mynode** ptail);

mynode *head=NULL,*tail=NULL;
int main()
{
    int nodes,i;
    int data;
    printf("how many nodes u want to insert ?\n");
    scanf("%d",&nodes);
    for(i=0;i<nodes;i++)
    {
        printf("Enter data\n");
        scanf("%d",&data);
        insert_data(data,&head,&tail);
    }
    print_list(head);
    return 0;
}
void insert_data(int data, mynode **phead,mynode ** ptail)
{
    mynode* ptr = malloc(sizeof(mynode));
    if(ptr==NULL){printf("malloc failed\n");exit(1);}
    ptr->info = data;  // ptr-> is equivalent to (*ptr).
    if((*phead) == NULL)
    {
        ptr->next = NULL;
        *phead = ptr;
        *ptail = ptr;
    }
    else
    {
        printf("inside else\n");
        ptr->next=NULL;
        (*ptail)->next = ptr;
        *ptail = ptr;
    }
    return;
}

void print_list(mynode* somelisthead)
{   
    if(somelisthead==NULL){printf("empty list\n");return;}
    mynode* ptr = somelisthead; // ptr is local : it only exist in this function.
    while(ptr->next != NULL)
    {
        printf("%d ",ptr->info);
        ptr = ptr->next;
    }   
    printf("%d\n",ptr->info);
}

我没有检查scanf()的返回值。但这将是另一个好主意......

关于c - C 中的段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160169/

相关文章:

在Linux中从一张图像更新创建电影

c++ - realloc 错误而不是 malloc?

java - 是否有任何数据结构可以保留迭代顺序并删除旧记录?

javascript - 检查用户是否分配给特定插槽

c - 我尝试添加链接列表时遇到问题,但调试器说无法访问临时内存

c - 函数指针签名不匹配,执行程序时仍然没有遇到任何问题

c - 如果给出了错误的输入,我的函数应该包含什么才能返回 -1.0 的值?

c++ - 用于整数下限和上限查询的快速数据结构?

python - Python 中链表中的“AttributeError”

C、双向链表问题