Thread: deleting a pointer that was not created with new

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    deleting a pointer that was not created with new

    Hi, i am wri
    Code:
    delete current;
                        current=0;
    ting a linked list and i would like to know how to delete a pointer that was not created with new.I have tried delete but it does not work.I know that in c , i use free();
    Code:
    void LinkedListCollection::remove(int item){
        intListElement *previous;
        intListElement *current;
        current=this->head_;
        if(lookup(item==1))
        {
            /*incase the item is in the head
             */
            if(current->num==item)
                this->head_=this->head_->next;
            else
            {
                previous=current;
                current=current->next;
                while(current!=0)
                {
                    if(current->num==item)
                    {
                        //i would like to delete my current but it wont work for me
                        previous->next=current->next;
                        //delete current;
                        current=0;
                    }
                    else
                        previous=current;
                        current=current->next;
                    
                    
                 }
            }//else
        }//if
        
        
    }
    Last edited by sigur47; 01-22-2012 at 05:01 AM. Reason: Code inserted

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i would like to know how to delete a pointer that was not created with new
    The answer is you don't.

    If you used malloc(), then you use free()
    If you used new, then you use delete.
    If you used new[ ] then you use delete[ ]
    Anything else is undefined behaviour.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Salem View Post
    > i would like to know how to delete a pointer that was not created with new
    The answer is you don't.

    If you used malloc(), then you use free()
    If you used new, then you use delete.
    If you used new[ ] then you use delete[ ]
    Anything else is undefined behaviour.
    i assigned objects to my pointer like something like this
    current=this->head_;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe you just need to set it to be a null pointer.
    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
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    Maybe you just need to set it to be a null pointer.
    Thats what i did i assingned the pointer to null but wont that cause memory leak because that memory has not be deallocated

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sigur47
    Thats what i did i assingned the pointer to null but wont that cause memory leak because that memory has not be deallocated
    The memory will be deallocated when the object that the pointer used to point to goes out of scope (or when the program ends, if the object has static storage duration).
    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

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    The memory will be deallocated when the object that the pointer used to point to goes out of scope (or when the program ends, if the object has static storage duration).
    I have run the code and my delete method does not work it still contains the item that i deleted.Can you spot any error in my code.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, this looks wrong: lookup(item==1)

    Maybe you intended to write: lookup(item) == 1

    What does lookup do, anyway?
    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

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    For starters, this looks wrong: lookup(item==1)

    Maybe you intended to write: lookup(item) == 1

    What does lookup do, anyway?
    Wow i am so sloppy and careless.Look up checks to see if the item is in the linked list before it goes to delete it.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Great, but aren't you already doing that with your current->num==item checks?
    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

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    For starters, this looks wrong: lookup(item==1)

    Maybe you intended to write: lookup(item) == 1

    What does lookup do, anyway?
    I have solved my problem but i might like to hear your own views on the way i implement my method

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    [QUOTE=laserlight;1084941]Great, but aren't you already doing that with your current->num==item checks?[/QUOTEThats true so i am repeating code twice on second thoughts

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting pointer array of pointer array
    By rodrigorules in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2010, 05:36 AM
  2. checking memory/pointer if it's valid before deleting
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2009, 08:06 PM
  3. deleting and re-assigning a global variable pointer
    By Canadian0469 in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2008, 03:47 PM
  4. Deleting void pointer
    By Bargi in forum C++ Programming
    Replies: 12
    Last Post: 07-19-2008, 04:09 PM
  5. Deleting and resizing a pointer array.
    By Terran in forum C++ Programming
    Replies: 9
    Last Post: 06-19-2008, 10:25 AM