简体   繁体   English

为什么在尝试编辑此链接列表时遇到段错误

[英]Why I am getting a Seg Fault When I Try to Edit This Linked List

I have a C data structure that consists of a nested series of linked lists. 我有一个C数据结构,它由一系列嵌套的链表组成。 Imagine it like this. 试想像这样。

[]->[]->[]->[]->[]
|
V
[]->[]->[]->[]->[]
|
V
[]->[]->[]->[]->[]
|
V
[]->[]->[]->[]->[]

Top Level Nodes have a structure like this 顶级节点具有这样的结构

typedef struct _StackTop
{
    struct _StackTop *next;
    StackNode *head;
} StackTop;

Where StackNode is a pointer to the head of the Lower Level List which has a structure like this 其中StackNode是指向下级列表的头部的指针,其结构如下

typedef struct _StackNode
{
  int number;
  struct _StackNode *next;
} StackNode;

For part of the business logic, I need to change which of the nodes the Top Level points to, ie change StackNode *head. 对于业务逻辑的一部分,我需要更改顶级指向的节点,即更改StackNode * head。

This seems reasonable. 这似乎是合理的。

However, when I try and execute the code as follows: 但是,当我尝试执行以下代码时:

*note that stackKing is the first Top Level Node *请注意,stackKing是第一个顶级节点

StackTop *currStackNode = &stackKing;

currStackNode->head = currStackNode->head->next;

I am getting a seg fault. 我遇到段错误。

At first, I figured the issue was with editing the Top Level Node. 最初,我认为问题出在编辑顶层节点。 However, if I set it to something else 但是,如果我将其设置为其他

currStackNode->head = NULL;

all is well. 一切都很好。 I can even change what it points to. 我什至可以更改它指向的内容。

currStackNode->head = currStackNode->next->head;

without issue. 没有问题。

I'm fairly inexperience with dynamically allocated data systems, so I'm really hoping there's something very obvious I'm doing wrong, but I haven't been able to narrow it down on google or SO. 我对动态分配的数据系统缺乏经验,所以我真的希望有很明显的地方我做错了,但是我无法将其范围缩小到google或SO。

I'm looking at the information produced by valgrind, and I'm still not 100% sure what it means 我正在查看valgrind产生的信息,但我仍然不确定100%是什么意思

==5038== Invalid read of size 4
==5038==    at 0x8048955: cachesim_access (in /home/rrollins/3056/Assignment-  5/assignment5/cachesim)
==5038==    by 0x40604D2: (below main) (libc-start.c:226)
==5038==  Address 0x4 is not stack'd, malloc'd or (recently) free'd
==5038== 
==5038== 
==5038== Process terminating with default action of signal 11 (SIGSEGV)
==5038==  Access not within mapped region at address 0x4

It sounds like maybe this means there is an error with my initialization, since I'm trying to access some memory that hasn't been correctly malloc'd? 听起来好像这意味着我的初始化有错误,因为我正在尝试访问未正确分配的某些内存?

Since it several comments and my own analysis indicate the initialization is the problem, here's that code as well 由于上面的注释和我自己的分析都表明初始化是问题所在,因此该代码也是如此

  i = 0;
  j = 0;
  StackTop *currStackNode = &stackKing;
  currStackNode->next = NULL;
  currStackNode->head = NULL;
  StackNode *innerCurrStackNode;
  //for each set
  while (i<numSets) {
    //create a linked list of StackNodes, one for each "way"
    j = 0;
    while (j<assoc) {
      if (j == 0) {
        currStackNode->head = malloc(sizeof(StackNode));
        innerCurrStackNode = currStackNode->head;
      } else {
        innerCurrStackNode->next = malloc(sizeof(StackNode));
        innerCurrStackNode = innerCurrStackNode->next;
      }
      //set this to negative 1, to show that no number is least recently used
      innerCurrStackNode->number = -1;
      innerCurrStackNode->next = NULL;
      j++;
    }
    currStackNode->next = malloc(sizeof(StackTop));
    currStackNode = currStackNode->next;
    currStackNode->next = NULL;
    currStackNode->head = NULL;
    i++;
  }
}

However, I have no problem "iterating" through each linked list. 但是,我没有问题通过每个链接列表“迭代”。 It's only trying to set this value that creates a serious issue 仅尝试设置此值会造成严重问题

currStackNode->head->next失败,而currStackNode->next->head却没有,向我表明currStackNodehead指针可能未正确初始化,或者指向了奇怪的地方,因此当上面的第一行尝试引用时head-> ,它会导致段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM