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 7664867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:24:48+00:00 2026-05-31T14:24:48+00:00

I create a class, which creates a new list of pointers to objects: FeatureSet::FeatureSet()

  • 0

I create a class, which creates a new list of pointers to objects:

FeatureSet::FeatureSet() {
    this->featuresList = new list<HaarFeature*>;
    globalCounter = 0;
}

Now I would like to delete all object from the list, and then delete the pointer to the list. My code is:

FeatureSet::~FeatureSet() {
    for (list<HaarFeature*>::iterator it = featuresList->begin(); it !=     featuresList->end(); it++) {
        delete *it;
    }
    delete featuresList; // this take a long time (more than half a minute)
}

My question is what is the best method to solve this problem? Does my approach is correct? Unfortunately, currently the last operation (deleting a pointer to the list) takes about half minute. List has about 250000 objects.

  • 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-05-31T14:24:49+00:00Added an answer on May 31, 2026 at 2:24 pm

    OK. So I performed some research. I just have tested four solutions. Results are presented in the table below: (I can’t put an image, please click the link)

    Comparing an efficiency of list and vector containers

    As you can see from the table, vector containing objects is the fastest solution. It is also true that storing pointers to objects is much slower than containing real objects.

    SUMMARY:

    • Sequential access to objects aggregated in list is slow, probably because it requires “jumping” from pointer to pointer and particular objects don’t have to be stored in consecutive memory cells
    • Sequential access to vector is fast, probably because objects are stored in linear, consecutive memory cells
    • Using iterator to access consecutive objects is faster than using indexes, what isn’t surprising
    • Storing pointers instead of real objects is much slower, especially during allocation and deallocation of container (heap allocation)
    • Independently from storing object type, deallocation of list is much slower than deallocating of vector

    Answering to question asked by @Emile Cormier – Why I would like to use pointer to container? This ensure me an access to vector/list elements even after deletion of parent class object. Please consider this part of code:

    class A{
    public:
    A(){
        this->test = new vector<int>;
        for(int i = 0; i < 10; i++){
            test->push_back(i);
        }
    }
    ~A(){
        cout << "object A is dead\n";
        test->clear(); // <--- COMMENTING this line allows to access to the vector's elements "after death" without copying
    }
    vector<int>* GetPointer(){
        return this->test;
    }
    private:
    vector<int>* test;
    };
    
    class B{
    public:
    B(){
        for(int i = 0; i < 10; i++){
            test.push_back(i);
        }
    }
    ~B(){
        cout << "object B is dead\n";
        test.clear();
    }
    vector<int> GetbyValue(){
        return test;
    }
    private:
    vector<int> test;
    };
    
    cout << "\nCASE A\n";
    A* instance = new A();
    vector<int>* local = instance->GetPointer();
    delete instance; //deletion before calling!!!
    //Access is impossible, because clear method in destructor destroys original vector, connected with pointer
    cout << "Printing A\n";
    for(int i = 0; i < local->size(); i++){
        cout << (*local)[i] << "\n";
    }
    cout << "\nCASE B\n";
    B* instance2 = new B();
    vector<int> local2 = instance2->GetbyValue();
    delete instance2; //deletion before calling!!!
    //Access is still possible, because vector has been copied to local variable
    cout << "Printing B\n";
    for(int i = 0; i < local2.size(); i++){
        cout << (local2)[i] << "\n";
    }
    cout << "THE END\n";
    

    When I don’t use a pointer to vector (case B), I although can access objects from vector after deletion of “B” class object BUT in reality I use a local copy of returned vector. Using a pointer to vector (case A) allows me to get an access to vector objects even after deletion of “A” class object, when I don’t call clear() method in destructor manually of course.

    Finally – thanks everyone for help. I think that my problem has been solved.

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

Sidebar

Related Questions

I am trying to create a generic class which new's up an instance of
I have a class which would create a bean and the bean has few
I have the following Class which basically creates a list of categories (called commands,
I have this class which has a double list template of a struct of
this is a question about which class does a free on shared pointers. So
I've created a custom module (which currently only defines a new Exception class), and
i already have a project with TreeNode class which creates a hierachy of nodes
I need to create a class which calculates the distance between two points. I
I intended to create a class which only have static members and static functions.
I have a class (simulation) which creates an instance of another class (GUI). Inside

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.