Note that there are some explanatory texts on larger screens.

plurals
  1. POMalloc pointer error with queue implementation
    primarykey
    data
    text
    <p>I'm trying to implement a queue in C++. The queue holds strings.</p> <p>I'm using the following code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdlib.h&gt; struct qnode { std::string data;//it stores a string qnode *link;//and has a pointer to the next node in the structure. }; class queue { private: qnode *front; // Pointer to first node. qnode *rear; // Pointer to last node. public: // Constructor. queue(); //Is the queue empty? bool empty() ; //peeks first element int first(std::string *c); // Adds element to the queue. int enqueue(std::string c); // Extracts an element from the queue. int dequeue(std::string *c); // Destructor. ~queue(); }; //////////// ACTUAL CODE ///////////// //Constructor, front and rear initially point to null. queue::queue() { front = NULL; rear = NULL; } // If front points to null then the queue is empty. bool queue::empty() { return (front == NULL); } int queue::first(std::string *c) { int res = 1; if (!empty()) { res = 0; *c = front-&gt;data; } return res; } //Function for adding an element to the end of queue. int queue::enqueue (std::string c){ int res = 1; qnode *tmp; // Auxiliar pointer. tmp = new qnode; //Auxiliar new node. if(tmp != NULL) { // If the queue is empty then we store the data in tmp and make its link point to null and front and rear become tmp. if(empty()){ tmp-&gt;data = c; tmp-&gt;link = NULL; front = tmp; rear = tmp; rear-&gt;link = tmp; } else { // If it is not, then last node's link point to tmp, we store the data in tmp and tmp's link point to null. rear-&gt;link = tmp; tmp-&gt;data = c; tmp-&gt;link = NULL; // Rear of the queue points to tmp now. rear = tmp; } res = 0; } return res; } // Function for extracting an element from the queue. int queue::dequeue(std::string *c){ int res = 1; // We make sure queue is not empty and access first node's data with the help of tmp. if(!empty()) { qnode *tmp; tmp = front; //The data extracted is stored in the character container c, passed by reference. *c = tmp-&gt;data; // Front now has to point to the next node and the old element has to be deleted. front = front-&gt;link; delete tmp; res = 0; } return res; } // Destructor queue::~queue() { // If it is already empty we don't have to do anything. if (empty()){ return; } qnode *tmp; // We take front element and delete it throught a tmp node till the queue is empty. while (!empty()){ tmp = front; front = front-&gt;link; delete tmp; } } //////////// MAIN /////////// int main(int argc, char *argv[]) { queue queue; std::string str2; queue.enqueue("hello"); queue.dequeue(&amp;str2); std::cout &lt;&lt; str2; } </code></pre> <p>Sadly, I get the following error:</p> <blockquote> <p>$ g++ -o issue issue.cpp $ ./issue issue(23060,0x7fff71058310) malloc: <strong>* error for object 0x7f962b403920: pointer being freed was not allocated *</strong> set a breakpoint in malloc_error_break to debug helloAbort trap: 6</p> </blockquote> <p>If I comment the queue destructor (the "while" block), the implementation works fine. The implementation also worked fine using char elements (just single letters).</p> <p>Can anyone enlighten me?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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