【问题标题】:Access Violation Writing in a 2 way queue访问冲突写入 2 路队列
【发布时间】:2012-11-03 20:33:41
【问题描述】:

我正在尝试使用 c++ 创建一个两侧队列。 我正在使用 Visual Studio 2012 并不断获得:

First-chance exception at 0x00D95A29 in Console_Assignment1.exe: 0xC0000005: Access violation writing location 0x00000008.

我认为我遇到了指针问题(可能试图取消引用我不应该引用的东西)。 到目前为止,我发现问题的运气为零,我非常感谢您再看看。

(代码太长无法粘贴,所以我将复制我认为给我带来问题的函数。) 也许只是一个小小的概述。我有一个节点类,它包含两个指向节点的指针(下一个和上一个)和一个 int (值)。和一个队列类,它包含两个指向节点(第一个和最后一个)和一个 int(大小)的指针。

// enqueueBeg - adds a new node at the beginning of the queue.
void DBL_Queue::enqueueBeg(int insert_val)
{
node* new_node = new node(insert_val);  // Creates the new node.
new_node->setNext( this->getFirst() ); // Connects the new node to the first in the queue
this->getFirst()->setPrev( new_node ); // Connects the first node in the queue to the new one
this->setFirst( new_node );             // Sets the new node as the first in the queue
this->setSize ( this->get_queue_size() + 1 ); // adds 1 to the size of the list

// dequeueBeg - removes the first node of the queue.
int DBL_Queue::dequeueBeg()
{
int ret_value = this->getFirst()->getVal();
node* old_node = this->getFirst();
this->setFirst( this->getFirst()->getNext() ); // Sets the second node in the queue as the first.
this->getFirst()->setPrev( NULL ); // Removes the link between the new first new and the old one.
this->setSize( this->get_queue_size() - 1); // Removes 1 from queue size
delete old_node;  // Deletes the node that use to be first.
return ret_value; // Returns the value of the old node.

// DBL_Queue Destructor
DBL_Queue::~DBL_Queue()
{
if (this->first == NULL)   // if queue is empty do nothing
    return;
else 
{
    while (this->first->getNext() != NULL)  // go through all nodes and delete them one by one
    {
        node* deletion = this->getFirst();
        this->setFirst( this->getFirst()->getNext() );
        delete deletion;
    }
}
}

提前感谢您的帮助!

【问题讨论】:

  • 为什么?你发现std::deque 有问题吗?
  • 你试过在调试器中运行吗?它将帮助您找到崩溃的位置,并让您检查变量以帮助您了解可能导致崩溃的原因。但是,您是否想过当您将 first 节点排入队列时会发生什么,这意味着没有 current 第一个节点(即this->getFirst() 返回NULL)?出队函数也有类似的问题。
  • 我认为您将不得不发布所有代码。你很可能有多个问题。问题也很可能与您认为的不同。建议总是一样的,发布 smallestcompletecompilable 程序,你可以管理它仍然有你试图解决的错误。换句话说,做一些工作来删除不相关的东西,但要发布所有内容。

标签: c++ queue visual-studio-2012


【解决方案1】:

我认为这是你的问题:

 while (this->first->getNext() != NULL)  // go through all nodes and delete them one by one
{
    node* deletion = this->getFirst();
    this->setFirst( this->getFirst()->getNext() );
    delete deletion;
}

当你删除最后一个节点时,你会调用

this->setFirst( null );

因为this->getFirst()->getNext() 将为空,对吗? 那么while(this->first->getNext() 就变成了null->getNext()

为什么不直接

while(this->first != NULL)

?

编辑:除非你真的关心最小化析构函数的运行时间,否则为什么不

while(this->getFirst() != NULL) {this->dequeueBeg;}

【讨论】:

  • 这可能是一个问题,但不幸的是不是导致我的问题的问题。即使在修复它之后,我仍然会遇到同样的异常。
  • @Ned 虽然这不是 current 崩溃的原因,但您的代码中有 许多 类似的问题(使用指针而不检查它们是否NULL)。
【解决方案2】:

约阿希姆的评论: “你试过在调试器中运行吗?它可以帮助你定位崩溃的位置,还可以让你检查变量以帮助你了解可能导致它的原因。但是,你有没有想过当你加入第一个节点时会发生什么, 表示当前没有第一个节点(即 this->getFirst() 返回 NULL)?出队函数也有类似的问题。”

是解决方案。我的问题是我没有正确处理插入空队列或删除最后一个节点。

谢谢大家!

【讨论】:

    猜你喜欢
    • 2021-05-21
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 2020-09-04
    • 2012-12-05
    • 2013-04-15
    • 1970-01-01
    相关资源
    最近更新 更多