标识链表中的节点是否为空

问题描述 投票:0回答:1

我很难确定链表中的节点是否为空。我收到了一个[[Thread 1:EXC_BAD_ACCESS(code = 1,address = 0x34)错误,但它并不一致。有时会标识该节点为空,但有时在测试时会显示其具有地址。我在尝试调试后附加了一个映像,并设置了条件以查看节点是否为空

enter image description here

我一直在尝试多种方法来避免这种不良访问,但是不知道该怎么办。这可能不是最好的编码,但是任何帮助都可以帮助

struct InstructionNode *node = new InstructionNode; if(temp_head == NULL){ temp_head = node; temp_tail = node; temp = node; node = NULL; } else{ temp_tail->next = node; temp_tail = node; } condition_flag = 1; Token t = lexer.GetToken(); if(t.token_type != IF) syntax_error(); else{ node_tail->type = CJMP; condition(); condition_flag = 0; body(); } if(temp!= NULL){ if(temp->next != NULL){ node_tail->next = temp; //Bad access node_tail = temp; temp = temp->next; } else{ free(temp_head); } } else{ free(temp_head); } }

我的课堂演示

class demo{ public: demo(){ head = NULL; tail = NULL; node_head = NULL; node_tail = NULL; temp_head = NULL; temp_tail = NULL; temp = NULL; } void syntax_error(); variables *head, *tail; struct InstructionNode *node_tail, *temp_head,*temp_tail, *temp;

我将如何解决此问题。谢谢
c++ linked-list exc-bad-access
1个回答
0
投票
[如果我正确理解您的意思(node_tail->next = temp;行提供了错误的访问错误),可能是因为您检查temp是否不等于NULL,但不是node_tail不等于[ C0],然后尝试取消引用。如果由于某些原因NULLnode_tail(例如NULL运行并且对syntax_error()不执行任何操作),它将崩溃。

要解决此问题,您需要先检查并确保node_tail不是node_tail,然后才取消引用,而不仅仅是NULL

© www.soinside.com 2019 - 2024. All rights reserved.