I have written a working templated linked list that can hold a number of Types. Each of which are created with new before sending them to the InsertNode function.
CMyType* pMyType1 = new CMyType; // Create a new MyType*
pMyType1->m_iData = 5; // Hold a value
CNode<T>* pNewNode = new CNode<T>(pData);
Should I delete just the node? or the data then the node or what? I have run some tests and the following runs fine
delete pNode->GetData(); // inline just returns m_pData (calls destructor of MyType)
delete pNode; // (calls destructor)
Is this ok to do. I tried just deleting ONLY the node and in the debugger it shows m_pData going null too. But since I new'd the pMyType in main shouldn't I delete that somewhere as well?
I look forward to your reply.
Everytime you say new, there should be a corresponding delete. Or better yet, you should use smart pointers to save yourself the trouble of figuring out when and where to release memory or worry about forgetting to do it.
thanks (that was fast btw)