Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7916599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:52:00+00:00 2026-06-03T14:52:00+00:00

I’m doing some tests with a std::list of pointers. I’m using remove_if algorithm to

  • 0

I’m doing some tests with a std::list of pointers. I’m using remove_if algorithm to eliminate some elements of the list. But I encountered some problems, remove_if is creating memory leaks because it doesn’t destroy the pointers (I think).

I found a solution, but I don’t know if it is well made, correct or at least acceptable…

Here is the code:

#include <algorithm>
#include <iostream>
#include <list>

using namespace std;

class Object
{
 private:
         int intData;   
 public:
        Object(int n) : intData(n) { };
        int getIntData(void) { return intData; };
        void setIntData(int n) { intData = n; };
};

/** Functor */
struct listFunctor
{ 
 bool operator()(Object* obj1, Object* obj2) const
 {
  return (obj1->getIntData() < obj2->getIntData());
 }
};

class removeFunctor
{
 private:
         int remover;
 public:
        removeFunctor(int n) : remover(n) { };
        bool operator()(Object* obj)
        {
         bool res = (obj->getIntData() != remover);

         if(res)
          delete obj;

         return res; 
        }
};

typedef list<Object*> objList;
typedef list<Object*>::iterator objectListIter;

int main(int argc, char** argv)
{
 objList objectList;

 objectList.push_back(new Object(8));
 objectList.push_back(new Object(0));
 objectList.push_back(new Object(2));

 /** sort elements. */
 objectList.sort(listFunctor());

 /** print. */
 for(objectListIter it = objectList.begin(); it != objectList.end(); ++it)
  cout<<*it<<"  "<<(*it)->getIntData()<<'\n';   

 /** remove. */
 objectListIter iter = remove_if(objectList.begin(), objectList.end(), removeFunctor(8));

 /** print. */
 for(objectListIter it = objectList.begin(); it != iter; ++it)
  cout<<*it<<"  "<<(*it)->getIntData()<<'\n';   

 /** delete list. */
 for(objectListIter it = objectList.begin(); it != iter; ++it)
  delete *it;   

 objectList.clear(); //IS THIS NECESSARY?

 return 0;
}

The program first creates the list, sort it an then removes some elements.

Is this code a good and viable solution to this problem? Valgrind’s default scan doesn’t report any problems but I’m doing more tests.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:52:02+00:00Added an answer on June 3, 2026 at 2:52 pm

    Removing from a list<Object*> will only remove the pointers from the list. You should prefer a list<unique_ptr<Object>> or a list<shared_ptr<Object>> which will automatically delete the objects pointed to when the smart pointers are removed from the list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.