c - 在 chars.exe : 0xC0000005: Access violation reading location 0x00000004 中的 0x011414CE 抛出异常

标签 c pointers memory doubly-linked-list

所以我正在为一个使用 C 的程序编写一个代码,它只执行基本的链表任务,比如创建列表、在给定的当前节点之后插入一个节点、删除节点等。

我使用的是 visual studio 2015,因此当我尝试通过调试并使用 visual studio watch 可视化我的列表以及它是否在当前节点之后正确插入节点来测试我的 insert-a-node-after 函数时,我得到了错误:

在 chars.exe 中的 0x011414CE 处抛出异常:0xC0000005:访问冲突读取位置 0x00000004。

chars.exe 中 0x011414CE 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000004。

所以这是我的带有结构和列表声明的 .h 文件

#ifndef DLIST_H
#define DLIST_H


typedef struct DListNode_struct {
char *str;
int blankIndex;
int blankLength;
struct DListNode_struct *next;
struct DListNode_struct *prev;
} DListNode;

typedef struct DList_struct {
   int size;
   DListNode *head;
   DListNode *tail;
} DList;


void DListConstruct(DList* list);

void DListInsertAfter(DList* list, DListNode* currNode, DListNode* newNode);

#endif // DLIST_H

所以这是我的主要功能

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

int main() {

DList* list = NULL;
DListNode* currNode = NULL;

DListNode* newNode1 = (DListNode*)malloc(sizeof(DListNode));

DListInsertAfter(list, currNode, newNode1);



return 0;
}

这是我的 .c 文件,其中包含我的 insertafter 函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dlist.h"


void DListConstruct(DList* list) {

list->head = NULL;
list->tail = NULL;
list->size = 0;

}



void DListInsertAfter(DList* list, DListNode* currNode, DListNode* newNode) {

DListNode* sucNode;

if (list->head == NULL) {
    list->head = newNode;
    list->tail = newNode;
}

else if (currNode == list->tail) {
    list->tail->next = newNode;
    newNode->prev = list->tail;
    list->tail = newNode;
}

else {
    sucNode = currNode->next;
    newNode->next = sucNode;
    newNode->prev = currNode;
    currNode->next = newNode;
    sucNode->prev = newNode;
}
}

我发现了一些与我的问题相关的帖子,但所有这些帖子要么不针对链表,要么不在 C 中。

所以谁能帮我找到错误在哪里?

提前致谢。

最佳答案

您正在将 NULL 作为 list 传递给 DListInsertAfter 并首先检查 (list->head == NULL) 访问空指针时失败。

关于c - 在 chars.exe : 0xC0000005: Access violation reading location 0x00000004 中的 0x011414CE 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781059/

相关文章:

c++ - 如何在不重建数组的情况下将 vector<vector<int>> 转换为 int**?

c - 意外的指针随机变化。为什么会改变呢?破坏指针算术

c# - 在 C# 中显式释放内存

c++ - 185000阵列后BFPRT(中位数中值)算法崩溃

c - 向路由器发送命令请求并以编程方式获得其回复

c - 进行 URL 匹配和标签提取的有效方法是什么?

c++ - 为什么结构的 sizeof 不等于每个成员的 sizeof 之和?

c - 为什么连续声明的指针变量是按照地址递减的顺序存储在内存中的?

c++ - 在模板函数中自动将指针/迭代器转换为 const

c - 多个结构的预分配 - 相同的结构