P. 1
Special Edition Using Visual C++ 6

Special Edition Using Visual C++ 6


|Views: 77|Likes:
Published by aniket_nd

More info:

Published by: aniket_nd on Feb 28, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less






There is one other time when you need to iterate over a list. That's when the program is
about to terminate and you need to delete all the objects pointed to by the pointers in
the list. The List application performs this task in the view class's destructor, as shown
in Listing F.9.

Listing F.9 CMyListView Destructor


// Iterate over the list, deleting each node.
while (!list.IsEmpty())

CNode* pNode = (CNode*)list.RemoveHead();
delete pNode;



The destructor in Listing F.9 iterates over the list in a while loop until the IsEmpty()
member function returns TRUE. Inside the loop, the program removes the head node from
the list (which makes the next node in the list the new head) and deletes the node from
memory. When the list is empty, all the nodes that the program allocated have been

CAUTION: Don't forget that you're responsible for deleting every node
that you create with the new operator. If you fail to delete nodes, you
might cause a memory leak. In a small program like this, a few wasted bytes
don't matter, but in a long-running program adding and deleting hundreds
or thousands of list nodes, you could create serious errors in your program.
It's always good programming practice to delete any objects you allocate in

]TIP: Chapter 24, "Improving Your Application's Performance," discusses
memory management and preventing memory leaks.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->