• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2357
  • Last Modified:

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

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 4.9.9.2

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

using namespace std;

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

testing::testing(const char* nm)
{
      strcpy(name,nm);
}

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

void testing::nameset(const char* nm)
{
      strcpy(name,nm);
}

using std::list;

int main(int argc, char *argv[])
{
      testing *element;
      list<testing> ListElements;
      list<testing>::iterator p;
      
      element = new testing("Tony");
      
      ListElements.push_back(*element);
      
      element = new testing("Bill");
      ListElements.push_back(*element);
      
      p = ListElements.begin();
      
      p->display();
      p++;
      p->display();
      
      
    system("PAUSE");
    return EXIT_SUCCESS;
}
0
navigator010897
Asked:
navigator010897
  • 3
2 Solutions
 
InteractiveMindCommented:
  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. :)
0
 
InteractiveMindCommented:
Perhaps just   ListElements.clear();   is what you're after?
0
 
InteractiveMindCommented:
And   delete element; delete p;    // ?
0
 
danielssonCommented:
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;
listElements.push_back(*pTesting);

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;
listElements.push_back(pTesting);

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,
danielsson
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now