deleting a pointer that was not created with new

This is a discussion on deleting a pointer that was not created with new within the C++ Programming forums, part of the General Programming Boards category; Hi, i am wri Code: delete current; current=0; ting a linked list and i would like to know how to ...

  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 04:01 AM. Reason: Code inserted

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,744
    Maybe you just need to set it to be a null pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,744
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,744
    For starters, this looks wrong: lookup(item==1)

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

    What does lookup do, anyway?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,744
    Great, but aren't you already doing that with your current->num==item checks?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21