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



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

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

#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("%dn", 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;
   }
}

来自listtest.c的代码(我没有写这个):

#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 = %dn", listLength(head));
  printf("filled stack: ");
  printList(stack);
  printf("stack size = %dn", listLength(stack));
  for(i = -4; i < 25; i+=4)
  {
    n = removeItem(&head, i);
    if(!n) printf("remove did not find %dn", i);  
  }
  printf("list after removes: ");
  printList(head);
  printf("list length = %dn", listLength(head));
  for(i = 0; i < 5; ++i)
  {
    printf("pop: %dn", pop(&stack));
  }
  printf("stack after pops: ");
  printList(stack);
  printf("stack size = %dn", listLength(stack));
  reverseList(&head);
  printf("list after reverse: ");
  printList(head);
  freeList(head);
  head = NULL;
  freeList(stack);
  stack = NULL;
  return 0;
}

根据Valgrind和GDB的说法,分段错误是由主要的东西引起的。瓦尔格林德给了我一个错误:

 Access not within mapped region at address 0x6FFFFFFE3
==6300==    at 0x400953: main 

我的问题是究竟是什么导致了分段错误,我该如何解决它,以及我的代码中的任何其他内容都会导致分段错误吗?我不允许编辑listtest.c,所以任何更改都必须在linkedlist.c中。谢谢。

  1. struct ListNode *newNodeCN;newNodeCN->data = inputData;实际未分配新节点

  2. int listLength(struct ListNode* head) - i 从不初始化。

  3. oid freeList(struct ListNode* head) - free,然后取消引用

在函数 insertSort 中,条件 if (newNodeIS->data...) 取消引用指针而不验证它不是 NULL,那就是分段错误的根源。尝试修复报告返回:)

函数中的第一个 createNode,u 使用元素 "data",但没有为 newNodeCN malloc malloc 一个真正的内存,其类型是结构 ListNode。 此外,"分段错误"通常在您引用错误的内存地址(例如指针u不是malloc,或数组中的地址等)时出现。

相关内容

  • 没有找到相关文章

最新更新