Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess Violation Writing in a 2 way queue
    text
    copied!<p>I'm trying to create a two sided queue with c++. I'm using Visual Studio 2012 and keep getting: </p> <p><code>First-chance exception at 0x00D95A29 in Console_Assignment1.exe: 0xC0000005: Access violation writing location 0x00000008.</code></p> <p>I think i'm having a pointer issue (might be trying to dereference something i shouldn't). so far i have had zero luck finding the problem and i'd really appreciate a second look.</p> <p>(The code is too long to paste so i'll just copy the functions i think are giving me the problem.) Maybe just a small overview. I have a node class that holds two pointer to node (next and previous) and an int (value). and a queue class that holds two pointers to node (first and last) and an int (size).</p> <pre><code>// 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-&gt;setNext( this-&gt;getFirst() ); // Connects the new node to the first in the queue this-&gt;getFirst()-&gt;setPrev( new_node ); // Connects the first node in the queue to the new one this-&gt;setFirst( new_node ); // Sets the new node as the first in the queue this-&gt;setSize ( this-&gt;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-&gt;getFirst()-&gt;getVal(); node* old_node = this-&gt;getFirst(); this-&gt;setFirst( this-&gt;getFirst()-&gt;getNext() ); // Sets the second node in the queue as the first. this-&gt;getFirst()-&gt;setPrev( NULL ); // Removes the link between the new first new and the old one. this-&gt;setSize( this-&gt;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-&gt;first == NULL) // if queue is empty do nothing return; else { while (this-&gt;first-&gt;getNext() != NULL) // go through all nodes and delete them one by one { node* deletion = this-&gt;getFirst(); this-&gt;setFirst( this-&gt;getFirst()-&gt;getNext() ); delete deletion; } } } </code></pre> <p>Thanks in advance for the help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload