delete a pointer held by a list container

This is a discussion on delete a pointer held by a list container within the C++ Programming forums, part of the General Programming Boards category; example Code: aClass * aObject = new aClass; std::list<aClass> aClassList; aClassList.push_back(aObject); //do lots of stuff passing the list back and ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    Question delete a pointer held by a list container

    example
    Code:
    aClass * aObject = new aClass;
    
    std::list<aClass> aClassList;
    
    aClassList.push_back(aObject);
    
    //do lots of stuff passing the list back and forth among functions
    
    aClassList.erase();
    the problem is that aObject doesn't get deleted.

    How do I solve this?

    Thanks for any help.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    What you store is just a list of *pointers* to objects, so when you "erase," the pointers are deleted but the objects still there.

    You need to loop thru the list and "delete" each object, then you can "erase" the list.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    Your list does not contain objects--it contains pointers to objects. When you use erase(), the pointers are removed from the list. Removing pointers from a list does nothing to the original objects.

    If you had an array of pointers to objects, how would you delete the objects those pointers point to? Or, more simply: if you have a pointer to one object, how would you delete the object?

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    I have tried the following but it did not work.

    Code:
    std::list<aClass>::iterator iaClass = aClassList.begin();
    
    while(iaClass != aClassList.end())
    {
         delete &iaClass;
         iaClass++;
    }
    I guess i'm not sure how to access the object pointed to by iaClass.

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    should be this
    Code:
    delete *iaClass;
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Quote Originally Posted by alphaoide
    should be this
    Code:
    delete *iaClass;
    I get the following error.

    C:\unzipped\binary\main.cpp(183) : error C2440: 'delete' : cannot convert from 'class aClass' to ''

    Just in random attempts, I have tried many combinations.

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by nineofhearts
    I have tried the following but it did not work.

    Code:
    std::list<aClass>::iterator iaClass = aClassList.begin();
    
    while(iaClass != aClassList.end())
    {
         delete &iaClass;
         iaClass++;
    }
    The problem with this code is that your list doesn't hold pointers.
    If it would contain pointers use
    Code:
         delete *iaClass;

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Hmm, maybe you have to cast
    Code:
    delete reinterpret_cast<aClass *>(*iaClass);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by alphaoide
    Hmm, maybe you have to cast
    Code:
    delete reinterpret_cast<aClass *>(*iaClass);
    No. Just put pointers into the list ( otherwise there is no need to delete anything )
    Code:
    aClass * aObject = new aClass;
    std::list<aClass*> aClassPtrList;
    aClassList.push_back(aObject);
    for ( std::list<aObject*>::iterator i= aClassPtrList.begin(); i != aClassPtrList.end(); ++i )
       delete *i;
    aClassPtrList.erase();

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I guess i'm not sure how to access the object pointed to by iaClass.
    iaClass is a horrible non-descriptive name as is aObject since you do this:

    aClass * aObject = new aClass;

    aObject is not an object--it is a pointer to an object. I think if you used some descriptive variable names, you might get things straightened out.

    An interator acts like a pointer to the things in your list. So, iaClass acts like a pointer to things in your list. To get the things in your list, you need to dereference iaClass.

    Code:
    I get the following error.
    
    C:\unzipped\binary\main.cpp(183) : error C2440: 'delete' : cannot convert from 'class aClass' to ''
    
    Just in random attempts, I have tried many combinations.
    Post all the code that produces that error.
    Last edited by 7stud; 12-17-2005 at 11:55 AM.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Quote Originally Posted by ZuK
    Code:
    aClass * aObject = new aClass;
    std::list<aClass*> aClassPtrList;
    aClassList.push_back(aObject);
    for ( std::list<aObject*>::iterator i= aClassPtrList.begin(); i != aClassPtrList.end(); ++i )
       delete *i;
    aClassPtrList.erase();

    Zuk, this solved the problem. Thank you sooooo much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 03:30 PM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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