We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,166 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Should I delete the DATA from a linked list node?

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.
in Main:

CMyType* pMyType1 = new CMyType;  // Create a new MyType*
pMyType1->m_iData = 5;            // Hold a value
LLMyTypes.InsertAtEnd(pMyType1);

in CLinkedList::InsertAtEnd(...)

CNode<T>* pNewNode = new CNode<T>(pData);
...
m_uNodeCount++;

Should I delete just the node? or the data then the node or what? I have run some tests and the following runs fine
within CLinkedList::DeleteNode(...)

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.

2
Contributors
2
Replies
10 Minutes
Discussion Span
3 Months Ago
Last Updated
3
Views
Elixir42
Light Poster
49 posts since Oct 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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.

deceptikon
Challenge Accepted
Administrator
3,452 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

thanks (that was fast btw)

Elixir42
Light Poster
49 posts since Oct 2012
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0618 seconds using 2.68MB