Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 26th, 2005, 2:49 PM   #1
xaosai
Newbie
 
xaosai's Avatar
 
Join Date: Mar 2005
Posts: 8
Rep Power: 0 xaosai is on a distinguished road
memory leak question

I'm building a linked list class and have a question about one of my memeber functions. This is what i have now:
//Removes the last item from a linked list
void linked_list::pop_tail()
{
     ll_item *cursor;
     cursor = head;

     while (true)
     {
          if(cursor->next_item->next_item == NULL)
          {
               delete cursor->next_item;
               cursor->next_item = NULL;

               break;
          }

          cursor = cursor->next_item;
      }
size--;
}

It works fine but I'm curious about the necessity of the first statement inside the if clause. Do I really need to delete the memory contents of next_item if I'm setting it to null, i.e. can I get away with just setting it to null without a memory leak?
xaosai is offline   Reply With Quote
Old Mar 26th, 2005, 3:47 PM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
if you free next_item you'll have to reallocate next time you want to use it, so you can just leave it NULL, it should be allocated already, so you should be fine.
Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 31st, 2005, 10:25 AM   #3
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4 mackenga is on a distinguished road
Ew, no! next_item is a pointer. Setting it to NULL will leave the memory allocated, sure, but you'll have no record of the address, meaning the structure exists but your program has no way of getting back at it. If you try to indirect through a null pointer, you'll get a Segmentation Fault (on Unix; some equivalent, probably more serious, and more vague error on Windows toys).

In other words, if you were to omit delete, you'd still need to allocate again next time with new, but you'd leak memory each time this happened. If you want to keep a pool of memory on the heap so you don't have to keep allocating and deallocating, you have to work harder than this and keep a list of references to your available allocated memory that you can look up - effectively implement your own mini memory manager on top of the existing one.
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC