Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess violation in doubly linked list
    text
    copied!<p>I posted earlier and got good help with figuring out how to deep copy my doubly linked list. I am now having a problem with an access violation "0xC000000005" which I believe is being caused by an attempt to deference a null pointer. This is a homework assignment and I am new to C++ so I'd just like some help figuring out where I am going wrong, as opposed to someone just giving me working code.</p> <p>Here is my interface, given to me by my professor. I can not modify it in any way.</p> <pre><code>#ifndef TEXTANALYZER_H #define TEXTANALYZER_H #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; class TextAnalyzer { private: /* * Class: Node * * This class represents a node in a sorted doubly linked list that stores a * list of words and their frequency of occurrency. */ class Node { public: string word; int wordFrequency; Node* previous; Node* next; Node(const string&amp; word, const int wordFrequency, Node* const previous, Node* const next) : word(word), wordFrequency(wordFrequency), previous(previous), next(next) {} }; // end ListNode /*********************************************************************/ Node* head; Node* tail; /* * Releases all the memory allocated to the list. */ void releaseNodes(); /* * Makes a deep copy of the object. */ void copyNodes(Node* const copyHead); /* * Returns a populated Node. * Throws a bad_alloc exception if memory is not allocated. */ Node* createNode(const string&amp; word, const int wordFrequency, Node* const previous, Node* const next); public: /* * Initializes head and tail, each to a dymmy node. */ TextAnalyzer(); /* * Makes a deep copy of the object passed in. * Calls copyNodes() to do the actual work. */ TextAnalyzer(const TextAnalyzer&amp; copyObject); /* * Releases all the memory allocated to the object. * Calls the releaseNodes() method to do the actual work. */ ~TextAnalyzer(); /* * Makes a deep copy of the rhs object. */ TextAnalyzer operator =(const TextAnalyzer&amp; assignObject); /* * Inserts the word in a sorted order into the list. * * If no Node exists with that initial character, one is added in * sorted order. If one does exist (same word), then the word frequency * of that word is incremented by one. */ void insertWord(const string&amp; word); /* * Returns a count of all the words in the list. */ int wordCount() const; /* * Returns a count of all the words with the initial character. */ int wordCountWithInitialCharacter(const char startsWith); /* * Returns a description of the object. The string is formatted as: * [A words:] * [&lt;word&gt;(&lt;count&gt;)] * [&lt;word&gt;(&lt;count&gt;)] * ... * * [B words:] * [&lt;word&gt;(&lt;count&gt;)] * [&lt;word&gt;(&lt;count&gt;)] * ... * *... */ string toString() const; }; #endif </code></pre> <p>Here is my class definition:</p> <pre><code>#include "textAnalyzer.h" #include &lt;string&gt; #include &lt;iostream&gt; #include &lt;sstream&gt; TextAnalyzer::Node* TextAnalyzer::createNode(const string&amp; word, const int wordFrequency, Node* const previous, Node* const next) { return new Node(word, wordFrequency, previous, next); } void TextAnalyzer::releaseNodes() { Node* del = tail; while(tail != NULL) { tail = tail-&gt;previous; tail-&gt;next = del; delete del; del = tail; } delete [] head; delete [] tail; head = tail = del = NULL; } void TextAnalyzer::copyNodes(Node* const copyHead) { head = new Node(*copyHead); Node* iter = head-&gt;next; for(Node* np = copyHead-&gt;next; np != NULL; np = np-&gt;next) { iter-&gt;next = new Node(*np); iter = iter-&gt;next; } iter = NULL; } TextAnalyzer::TextAnalyzer():head(createNode("0",0,NULL,NULL)),tail(head) {} TextAnalyzer::TextAnalyzer(const TextAnalyzer&amp; copyObject) { copyNodes(copyObject.head); } TextAnalyzer::~TextAnalyzer() { releaseNodes(); } TextAnalyzer TextAnalyzer::operator=(const TextAnalyzer&amp; assignObject) { return TextAnalyzer(assignObject); } void TextAnalyzer::insertWord(const string&amp; word) { Node* iter = head-&gt;next; while(iter != NULL) { if(iter-&gt;word == word) iter-&gt;wordFrequency++; else if(iter-&gt;word[0] == word[0] &amp;&amp; iter-&gt;next != NULL) { Node* temp = iter-&gt;next; iter-&gt;next = createNode(word, 1, iter, temp); iter = iter-&gt;next; temp-&gt;previous = iter; temp = NULL; } else if(iter-&gt;word[0] == word[0] &amp;&amp; iter-&gt;next == NULL) { iter = createNode(word, 1, tail, NULL); tail = iter; } else iter = iter-&gt;next; } iter = NULL; } int TextAnalyzer::wordCount() const { Node* iter = head-&gt;next; int count = 0; while(iter != NULL) count++; return count; } int TextAnalyzer::wordCountWithInitialCharacter(const char startsWith) { Node* iter = head-&gt;next; int count = 0; for(int i = 0; i &lt; wordCount(); i++) { if(startsWith == iter-&gt;word[0]) count++; iter-&gt;previous = iter; iter = iter-&gt;next; } iter = NULL; return count; } string TextAnalyzer::toString() const { Node* iter = head-&gt;next; string desc = "List of words: \n"; ostringstream convert; for(int i = 0; i &lt; wordCount(); i++) { convert &lt;&lt; iter-&gt;word[0] &lt;&lt; " words:\n" &lt;&lt; iter-&gt;word &lt;&lt; "(" &lt;&lt; iter-&gt;wordFrequency &lt;&lt; ")\n"; iter-&gt;previous = iter; iter = iter-&gt;next; } iter = NULL; return desc + convert.str(); } </code></pre> <p>The problem occurs in my releaseNodes() method according to the debugger. Ive added a comment to it to point out the specific line it occurs on:</p> <pre><code>void TextAnalyzer::releaseNodes() { Node* del = tail; while(tail != NULL) { tail = tail-&gt;previous; //debugger flags this line when error occurs tail-&gt;next = del; delete del; del = tail; } delete [] head; delete [] tail; head = tail = del = NULL; } </code></pre> <p>Im not sure what is causing the access violation, but like I said I am new to C++. Any and all help is appreciated.</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