STL::List of pointers - proper way to delete?

Posted on 2006-05-08
Last Modified: 2009-07-29
Hi all,

I'm playing with the stl list feature, something I haven't used really.  Basically, I want to create a list of a given class, entries in the list will be dynamically created.  My problem is the deletion of a given pointer...

For example, with my experiment code... (yes, it is a bit sloppy, but it was quick and dirty to test theory)

I have a class testing that contains some data - no problem.  

I create a new testing object, and set the data - no problem.

I add the pointer to the object to the list, - no problem

I retrieve it for displaying using the iterator - no problem

I want to remove a given item and properly free the memory - problem

I need to get the pointer value out before removing it from the list so I can ultimately delete the object to release the memory.  This is where I am not sure how to extract the actual pointer back out of the list.

if I try (after setting p to the beginning):

element = p

I get a compiler error

main.cpp cannot convert `std::_List_iterator<testing>' to `testing*' in assignment

I'm just not sure how to proceed.  I realize I might be doing it just plain wrong with regard to setting up the list, I was thinking of creating the list as <testing *> but I wasn't sure what that would get me....

The following code does work and has been tested in dev-cpp

#include <cstdlib>
#include <list>
#include <iostream>

using namespace std;

class testing {
      char name[10];
            testing(const char* nm);
            void display();
            void nameset(const char* nm);

testing::testing(const char* nm)

void testing::display(void)
      cout <<name << endl;

void testing::nameset(const char* nm)

using std::list;

int main(int argc, char *argv[])
      testing *element;
      list<testing> ListElements;
      list<testing>::iterator p;
      element = new testing("Tony");
      element = new testing("Bill");
      p = ListElements.begin();
    return EXIT_SUCCESS;
Question by:navigator010897
    LVL 25

    Accepted Solution

      element = p ;

    I think would be:

       element = &p ;

    Whether or not that would help you solve this problem however, I do not know; as I'm not entirely clear on what you're trying to achieve -- I shall read over it a couple more times. :)
    LVL 25

    Expert Comment

    Perhaps just   ListElements.clear();   is what you're after?
    LVL 25

    Expert Comment

    And   delete element; delete p;    // ?
    LVL 2

    Assisted Solution

    We're having a minor mash up of heap and stack objects here. You have declared your list as follows:

    list<testing> listElements;

    Then you do the following:

    testing *pTesting = new testing;

    The thing is, by doing a push_back on *pTesting, you actually add a _copy_ of pTesting to the list. What you're really trying to do is:

    list<testing *> listElements;
    testing *pTesting = new testing;

    Then you can do a

    delete pTesting;

    by the time you want to free the object. With your previous method, there is no need to delete the objects in the list, they will be automatically destroyed when you remove a item in the list (or when the entire list is destroyed). In fact, you'd still have a memory leak as the pTesting variable you add by push_back(*pTesting) is never destroyed. Note that it would be perfectly alright to do a

    delete pTesting;
    pTesting = null;

    right after doing the push_back(*pTesting). Once more: You're adding a copy of the object to the list.

    Hope that helps,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now