【问题标题】:C++ Access violation reading location in single link list单链接列表中的 C++ 访问冲突读取位置
【发布时间】:2013-11-01 21:49:04
【问题描述】:

我正在使用 c++ 来实现单个整数链接列表。我的程序只是要求用户用 5 个整数填充列表,然后程序应该删除任何偶数并在删除后打印列表。

这是我的代码:

#include <iostream>
using namespace std;
class IntSLLNode
{
public:
    IntSLLNode()     { next = 0; }
    IntSLLNode(int i, IntSLLNode *ptr = 0)
    {
        info = i;
        next = ptr;
    }
    int info;
    IntSLLNode *next;
};
class IntSLList
{
public:
    IntSLList() {head = tail =0; }
    void AddToTail(int);
    void DeleteNode(int);
    void DisplayList();
    void deleteEven();
    IntSLLNode * getHead()
    {
        return head;
    }
private:
    IntSLLNode *head, *tail;
};
void IntSLList::AddToTail(int el)
{
    if (tail != 0) // if list not empty;
    {   tail->next = new IntSLLNode(el);
        tail = tail->next;
    }
    else
        head = tail = new IntSLLNode(el);
}

void IntSLList::deleteEven()
{
IntSLLNode *current;
current=head;
int  num;
while (current!=0)
{
    num=current->info;
    current=current->next;
    if(num%2==0)
    {
        DeleteNode(num);
    }
}
}
void IntSLList::DeleteNode(int el)
{
    if(head !=0)
        if(head==tail && el==head->info)
        {
            delete head;
            head=tail=0;
        }
        else if(el==head->info)
        {
            IntSLLNode *tmp=head;
            head=head->next;
            delete tmp;
        }
        else
        {
            IntSLLNode *pred, *tmp;
            for(pred=head, tmp=head->next;
                tmp!=0 && !(tmp->info==el);
                pred=pred->next, tmp=tmp->next);
            if(tmp!=0)
            {
                pred->next=tmp->next;
                if(tmp==tail)
                    tail=pred;
                delete tmp;
            }
        }
}

void IntSLList::DisplayList()
{
    IntSLLNode *current;
    current=head;
    if(current==0)
        cout<<"Empty List!";
    while (current!=0)
    {
        cout<<current->info<<" ";
        current=current->next;
    }
}

我在 ex4.exe 中的 0x002c1744 处遇到未处理的异常:0xC0000005: 声明 int num=current->info 中的访问冲突读取位置 0xfeeefeee;谁能建议如何解决这个问题?

【问题讨论】:

  • 也许您的代码需要正确格式化?
  • 将该错误视为一个不那么微妙的提示,即指针 current 中保存的地址被取消引用是虚假的。现在在你的代码中向后走,看看你是否能看到它是怎么回事。考虑一下您的DeleteNode 函数做了什么,以及它可能对该函数的调用者 意味着什么,在本例中为deleteEven()。当DeleteNode() 返回并且您又回到deleteEven() 时,您认为current 指的是什么?
  • num=current-&gt;info 线路在哪里?
  • 0xfeeefeee 表示您正在访问已释放的指针:stackoverflow.com/questions/127386/…
  • 删除的时候好像没必要再找int了,为什么不直接把指针传给DeleteNode呢?

标签: c++ visual-studio-2010


【解决方案1】:

对此我不确定,但我认为问题出在此处:

void IntSLList::deleteEven()
{
    IntSLLNode *current;
    current=head;
    while (current!=0)
    {
        if(current->info%2==0)
            DeleteNode(current->info);
        current=current->next; // this line
    }
}

我认为if 语句中执行的已删除节点与您的下一行之间存在关系。如果您删除指向 DeleteNode() 中某个元素的特定指针,那么您的 current 到目前为止可能会被删除,它将指向错误的地址。

编辑

void IntSLList::deleteEven()
{
    IntSLLNode *current;
    current=head;
    while (current!=0)
    {
        if(current->info%2==0)
        {
            int ind = current->info;

            current=current->next;
            DeleteNode(ind);
        }
        else
        {
          current=current->next;
        }
    }
}

【讨论】:

  • 非常感谢 MahanGM :) 我放置了 current=current->next;在删除之前,它工作得很好。我更新了我的代码。
  • @user1477701 如果这就是您所做的一切并且它“完美运行”,请考虑它是更多未定义的行为。移动那条线意味着您现在要删除节点要删除的节点之后,并且您仍然在下一次绕过时取消引用无效节点指针,只是现在 之前 而不是 DeleteNode 之后。这个答案是正确的,但如果按照您的描述实施,您的解决方案肯定不是
  • @user1477701 WhozCraig 说得对。您必须根据该 if 语句设置当前节点,但我再次不确定,因为您的代码实现错误。检查我的编辑。
猜你喜欢
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多