所以我正在为一个使用 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/