Access Violation Writing in a Queue with Two Queues

I am trying to create a two-way queue with C ++. I am using Visual Studio 2012 and keep getting:

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

I think I have a problem with the pointer (maybe you are trying to dereference something that I shouldn't). I’m still unlucky to find this problem, and I really appreciate the second look.

(The code is too long to insert, so I just copy functions that I think give me a problem.) Maybe just a short review. I have a node class that contains two pointers to node (next and previous) and int (value). and a queue class containing two pointers to node (first and last) and int (size).

 // 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; } } } 

Thanks in advance for your help!

+4
source share
2 answers

Joachim’s comment: “Have you tried working in the debugger? It will help you find the place where the failure occurred, and also allow you to look at the variables to help you understand what might cause it. However, have you thought about what happens when you queue the first node, which means there is currently no first node (i.e. this-> getFirst () returns NULL)? You have similar problems with the dequeueing function. "

There was a solution. my problem was that I did not handle inserting into an empty queue or deleting the last node correctly.

Thanks everyone!

0
source

I think this is your problem:

  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; } 

When you delete the last node, you call

 this->setFirst( null ); 

because this->getFirst()->getNext() will be null, right? So while(this->first->getNext() becomes null->getNext()

Why not just

 while(this->first != NULL) 

?

Edit: if you really don't care about minimizing the runtime of the destructor, why not

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

Source: https://habr.com/ru/post/1443869/


All Articles