运行C程序时出现分段错误

运行C程序时出现分段错误,c,pointers,struct,linked-list,segmentation-fault,C,Pointers,Struct,Linked List,Segmentation Fault,我一直在为CS类中的一个赋值编写C程序,该类编译成功,但运行时出现分段错误。任务涉及LinkedList;我们必须采用在名为linkedlist.h的头文件中描述和声明的方法,并在名为linkedlist.c的文件中实现它们。提供了一个名为listtest.c的文件,用于测试该方法 我的代码(linkedlist.c,注释来自我们收到的头文件,描述了这些方法应该如何工作): 我的问题是,到底是什么导致了分段错误,我如何修复它,我的代码中的其他内容是否会导致分段错误?我不允许编辑listtest.

我一直在为CS类中的一个赋值编写C程序,该类编译成功,但运行时出现分段错误。任务涉及LinkedList;我们必须采用在名为linkedlist.h的头文件中描述和声明的方法,并在名为linkedlist.c的文件中实现它们。提供了一个名为listtest.c的文件,用于测试该方法

我的代码(linkedlist.c,注释来自我们收到的头文件,描述了这些方法应该如何工作):


我的问题是,到底是什么导致了分段错误,我如何修复它,我的代码中的其他内容是否会导致分段错误?我不允许编辑listtest.c,因此任何更改都必须在linkedlist.c中进行。谢谢。

在函数insertSorted中,如果(newNodeIS->data…)在未验证指针是否为空的情况下取消指针的差异,则可能是分段错误的来源。 请尝试修复该报告:)

  • 结构ListNode*newNodeCN; newNodeCN->data=输入数据;没有实际分配新节点

  • int listLength(struct ListNode*head)-i从未初始化

  • oid自由列表(结构ListNode*head)-自由,然后取消引用


  • 首先在函数createNode中,u使用元素“data”,但没有为newNodeCN malloc一个实际内存,which的类型是struct ListNode。此外,“分段错误”通常出现在引用错误的内存地址时(例如指针u不是malloc,或者数组中的地址等等)。

    显然您没有使用调试符号编译(选项-g)。另外,您可能应该在insertSortedI之前调用createnode。我在newNode->data
    if(newNodeIS!=NULL){if(newNodeIS->data>listHeadIS->data){listHeadIS=newNodeIS;}
    但我仍然会遇到分段错误。通常这种错误会在整个代码中重复出现,请尝试检查并清除此特定错误,然后再次运行:)我更改了
    *newNodeCN
    to
    struct ListNode*newNodeCN=(struct ListNode*)malloc(sizeof(struct ListNode))并且我将listLength中的I初始化为0。这修复了分段错误,现在程序运行得更远,但随后遇到另一个分段错误。你说的免费然后取消引用是什么意思?freeList中的while循环不是这样做的吗?是的,但是你按错误的顺序做了:你释放()对象(因此它不再存在),然后你通过head->next请求它的成员!实际上,您也可以在removietem()中执行此操作。
    
    #include "linkedlist.h"
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /* Alloc a new node with given data. */
    struct ListNode* createNode(int inputData)
    {
    
        struct ListNode *newNodeCN;
        newNodeCN->data = inputData;
        return newNodeCN;
    }
    
    /* Insert data at appropriate place in a sorted list, return new list head. */
    struct ListNode* insertSorted(struct ListNode* head, int inputData)
    {
        printf("insertsorted started \n");
    
        struct ListNode * nextIS = NULL;
        struct ListNode * newNodeIS = NULL;
        struct ListNode * currIS = head;
        struct ListNode * listHeadIS = currIS;
        if (currIS == NULL)
        {
            listHeadIS = createNode(inputData);
            return listHeadIS;
        }
        while (currIS->next != NULL)
        {
            nextIS = currIS->next;
    
            if (currIS->data < inputData)
            {
                if (nextIS->data >= inputData)
                {
    
                    nextIS->next = createNode(inputData);
                    newNodeIS = nextIS->next;
                    if (newNodeIS->data > listHeadIS->data)
                    {
                        listHeadIS = newNodeIS;
                    }
                }
            }
            currIS = currIS->next;
        }
    
        return listHeadIS;
    }
    
    /* Remove data from list pointed to by headRef, changing head if necessary.
    * Make no assumptions as to whether the list is sorted.
    * Memory for removed node should be freed.
    * Return 1 if data was present, 0 if not found. */
    int removeItem(struct ListNode** headRef, int data)
    {
        struct ListNode * tempRem = *headRef;
        int filterVal = data;
    
        while (tempRem->next != NULL)
        {
            if (tempRem->data == filterVal)
            {
                free(tempRem);
                tempRem = tempRem->next;
                return 1;
            }
        }
    
    
        return 0;
    }
    /* Insert data at head of list, return new list head. */
    struct ListNode* push(struct ListNode* head, int data)
    {
        printf("push started \n");
        int dataPush = data;
    
        struct ListNode * tempPush = (struct  ListNode*)malloc(sizeof(struct ListNode));
        tempPush->data = dataPush;
        tempPush->next = head;
        *head = *tempPush;
        return tempPush;
    }
    
    /* Remove and return data from head of non-empty list, changing head.
    * Memory for removed node should be freed. */
    int pop(struct ListNode** headRef)
    {
        struct ListNode * tempPop = *headRef;
        int tempData;
    
        tempData = tempPop->data;
        free(tempPop);
        tempPop = tempPop->next;
    
        return tempData;
    }
    /* Return length of the list. */
    int listLength(struct ListNode* head)
    {
        int i;
        while (head->next != NULL)
        {
            i++;
            head = head->next;
        }
        return i;
    }
    /* Print list data on single line, separated with spaces. */
    void printList(struct ListNode* head)
    {
        printf("PrintList Started \n");
        if (head != NULL)
        {
    
            while (head->next != NULL)
            {
    
                printf("%d\n", head->data);
                head = head->next;
            }
        }
    }
    /* Free memory used by the list. */
    void freeList(struct ListNode* head)
    {
        while (head != NULL)
        {
            free(head);
            head = head->next;
        }
    }
    /* Reverse order of elements in the list */
    void reverseList(struct ListNode** headRef)
    {
        struct ListNode * origRL = *headRef;
        struct ListNode * nextRL = NULL;
        struct ListNode * prevRL = NULL;
        while (origRL->next != NULL);
        {
            nextRL = origRL->next;
            prevRL = origRL;
            origRL = nextRL;
            origRL->next = prevRL;
       }
    
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "linkedlist.h"
    
    int main(int argc, char** argv)
    {
      int i, n;
      struct ListNode* head = NULL;
      struct ListNode* stack = NULL;
    
      printf("empty list: ");
      printList(head);
    
      for(i = 0; i < 23; ++i)
      {
        n = (i*17+11) % 23;
        head = insertSorted(head, n);
        printf("sort succesful /n");
        stack = push(stack, n);
      }
    
      printf("filled list: ");
      printList(head);
      printf("list length = %d\n", listLength(head));
    
      printf("filled stack: ");
      printList(stack);
      printf("stack size = %d\n", listLength(stack));
    
      for(i = -4; i < 25; i+=4)
      {
        n = removeItem(&head, i);
        if(!n) printf("remove did not find %d\n", i);  
      }
    
      printf("list after removes: ");
      printList(head);
      printf("list length = %d\n", listLength(head));
    
      for(i = 0; i < 5; ++i)
      {
        printf("pop: %d\n", pop(&stack));
      }
    
      printf("stack after pops: ");
      printList(stack);
      printf("stack size = %d\n", listLength(stack));
    
      reverseList(&head);
      printf("list after reverse: ");
      printList(head);
    
      freeList(head);
      head = NULL;
    
      freeList(stack);
      stack = NULL;
    
      return 0;
    }
    
     Access not within mapped region at address 0x6FFFFFFE3
    ==6300==    at 0x400953: main