Thread: Question about delete

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Question about delete

    Hi, I am trying to implement a priority queue (minheap) using pointers and a struct for my nodes for an assignment and I was wondering about something. My class supports a function deleteMin() which basically gets the root address which I have stored and deletes the data and key and replaces them with those of the last node on the tree and then rearranges the tree from top to bottom with comparisons. My question is this: As far the last node is concerned from which I took the data I change the pointer of its father to him to NULL so basically that node is disconnected from the tree. However that memory is not freed, right? It's still associated with the program? If it is I was wondering how I can free it so that memory address can be used in the future. I was thinking to use the command delete [pointer to ex-last node] but this will just erase the pointer, right?

    I apologise for being so confusing

    example of code
    Code:
    /*lst is a ponter to struct node and currently points to the last most right node of the tree*/
    
    setLstNode(lst->father->l_ch);  //Function assigns the new last node to be the right child 
                                                      //of the father of the ex-last node 
    lst->father->r_ch=NULL;           //assigning NULL to right child of father
    delete lst;                                 //This probably doesnt do a thing right?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    More like:
    Code:
    node* temp = lst->father->r_ch; // temp points to the last node
    setLstNode(lst->father->l_ch);  // set the new last node
    delete temp;                    // deallocate memory for former last node
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    hmm thanks. After this that address contains random data so it must work, however I still need

    lst->father->r_ch=NULL;

    because I use the fact if it has a null pointer when going through the tree from top to bottom...
    By the way cant I just delete lst then , I mean both lst and temp show to the same address...
    Anyway thanks laserlight.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, I am more familiar with the array implementation of min/max heaps.

    By the way cant I just delete lst then , I mean both lst and temp show to the same address...
    lst points to your min node, right? If you delete lst, then lst->father is invalid. It may still exist in memory, but you can no longer reach it from lst. As such, you should save a pointer to the min node in a temporary, and then delete it when you are ready.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Aha, okay. Thanks a lot...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  2. Quick question about new and delete
    By megatron09 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2006, 12:20 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Class Bank(i have a question)
    By kuwait in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2003, 07:40 AM
  5. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM