Note that there are some explanatory texts on larger screens.

plurals
  1. POpointer being freed was not allocated for pointer assignment
    primarykey
    data
    text
    <p>I was trying to change a ListNode struct into a class format but am running into some issues when testing it.</p> <p>Getting a.out(7016) malloc: <strong>* error for object 0x7fff65333b10: pointer being freed was not allocated *</strong> set a breakpoint in malloc_error_break to debug</p> <pre><code>chainLink.hpp #ifndef CHAINLINK_H #define CHAINLINK_H using namespace std; #include &lt;iostream&gt; #include &lt;cstdlib&gt; template &lt;typename Object&gt; class chainLink { private: Object storedValue; chainLink *nextLink; public: //Constructor chainLink(const Object &amp;value = Object()): storedValue(value) { nextLink = NULL; } /* Postcondition: returns storedValue; */ Object getValue() { return storedValue; } /* Postcondition: sets storedValue = value */ void setValue(Object &amp;value) { storedValue = value; } /* Postcondition: sets nextLink to &amp;value */ void setNextLink(chainLink* next) { nextLink = next; } chainLink* getNext() { return nextLink; } ~chainLink() { delete nextLink; } }; #endif </code></pre> <p>My test file, assume includes</p> <pre><code>int main() { chainLink&lt;int&gt; x(1); cout &lt;&lt; "X: " &lt;&lt; x.getValue() &lt;&lt; " "&lt;&lt; endl; chainLink&lt;int&gt; y(2); cout &lt;&lt; "Y: " &lt;&lt; y.getValue() &lt;&lt; " "&lt;&lt; endl; chainLink&lt;int&gt;* z = &amp;y; cout &lt;&lt; &amp;y &lt;&lt; " " &lt;&lt; z &lt;&lt; endl; x.setNextLink(z); } </code></pre> <p>OUTPUT: X: 1 Y: 2 0x7fff65333b10 0x7fff65333b10 a.out(7016) malloc: <strong>* error for object 0x7fff65333b10: pointer being freed was not allocated *</strong> set a breakpoint in malloc_error_break to debug Abort trap: 6</p> <p>The error seems to be thrown by the setNextLink function.</p> <p>Any help greatly appreciated.</p>
    singulars
    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