写入位置c++链表时发生访问冲突
access violation writing location c++ linked list
我正在尝试编写一个函数,该函数将指针指向头节点,然后使用递归反转节点之间的链接方式。
下面是我的函数。
void reverseRec(digits *&head){
digits *temp = head;
if(temp->next = NULL){
head = temp;
return;
}
reverseRec(head->next);
digits * a = temp->next;
a->next = temp;
temp->next = NULL;
}
函数调用如下:
int main(){
digits * head = new digits(); // pointer that points to head of the node
head = NULL;
insert(head, 5, 1);
insert(head, 6, 2);
insert(head, 1, 3);
insert(head, 7, 1);
delList(head, 1);
reverseRec(head);
}
如果有帮助的话,我试着用while循环使用一个正常的反向函数,结果很好,我的代码是这样的:
void reverse(digits *&head){
digits * prev, * current, * next;
current = head;
prev = NULL;
while(current!= NULL){
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
我是C++的新手,我遇到了很多"访问违规写入位置"错误,直到现在我都不知道它是什么(我通常会修改代码,直到它被修复,但我从来都不知道是什么原因导致的)。
很抱歉,这真的是一个新手问题,这些访问违规写入位置错误是什么?我的代码出了什么问题?
当您试图写入或读取未经程序授权的内存区域时,会发生访问冲突错误。你能告诉我这个访问违规错误是从哪里来的吗?
实际上,在IF()
条件中,您的方程式Temp->Next为NULL。你可以尝试更改。。
if(temp->next = NULL)
至
if(!temp->next)
在main()中,更改头部分配。
head ->next= NULL;
相关文章:
- 链表中写入访问冲突的未知原因
- 为什么我在空指针错误(链表)中获取成员访问权限
- 在C++中删除双向链表的头节点后出现访问冲突异常
- 循环访问自定义双链表
- 如何通过不同的指针使用类的对象访问结构?(链表)(C++)
- 尝试从单向链表C++中删除单个节点时出现读取访问冲突
- 链表的访问冲突异常
- C++链表读取访问冲突
- 我如何能够访问双向链表中的嵌套结构
- c++ 访问单个链表中的特定值
- 存储类对象的C++模板链表-未处理的异常访问冲突读取位置
- 在C++中读取链表上的访问冲突
- 链表访问冲突写入位置
- 链表访问冲突
- 写入位置c++链表时发生访问冲突
- 打印链表-访问冲突c++
- 如何使用boost::iterator_facade来访问遗留链表
- c++哈希表和链表的访问冲突
- 如何访问C++链表中的对象数据
- 复制构造函数后链表中的访问冲突