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

  • Home
  • SEARCH
  • 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 7530325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:52:50+00:00 2026-05-30T04:52:50+00:00

I have a list defined like: std::list<int *> m_ilist; and I add int s

  • 0

I have a list defined like:

std::list<int *> m_ilist;

and I add ints to the list:

m_ilist.push_back (new int (x));

I want to destruct the vector and delete the memory that was allocated for each element.

Which one is better:

  1. Doing a loop through the list, calling delete on each iterator. After this calling clear():

    for (...) { delete *it; } m_ilist.clear ();
    
  2. Doing a loop but calling erase on the iterator:

    for (...) { delete *it; m_ilist.erase (); }
    

Better would be defined as quicker / faster / less processing.

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-05-30T04:52:52+00:00Added an answer on May 30, 2026 at 4:52 am

    Benchmark it. More importantly, benchmark it in your particular application. There is no point in us testing a synthetic runtime of both versions, when your mileage will vary, depending on what you do afterwards.

    The bottleneck is the nasty freeing and mallocing that goes on behind the curtains. Depending on the memory allocation pattern the remainder of your program exhibits, you will most likely get different results.

    Having that said; The difference is most likely negligible, so my gut says go for the second and save one loop if in doubt.


    Edit: Note that if you’re really concerned about speed, use an std::vector<int> instead of an std::list<int*>. The difference will be significantly larger than between the options you list.

    Edit 2: If you use an std::vector make sure to use variant 1 (with clear instead of many erases). In this case it does make a huge difference. If the elements are really huge (as opposed to int) you may want to actually put pointers (or smart pointers, for that matter) into the vector to minimise the copy overhead.

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

Sidebar

Related Questions

I have a 3D vector defined like this... std::vector<std::vector<std::list<Object*> > > m_objectTiles; I have
I have a class Foo defined like this: class Foo { public: Foo(int num);
I have a listView which defined like this in xml: <ListView android:id=@android:id/list android:layout_width=match_parent android:layout_height=match_parent
I have a class defined like so: public class Test { static <T> List<Class<T>>
I have my node defined something like: class LLNode { public: std::shared_ptr<LLNode> prev; std::shared_ptr<LLNode>
I have a vector like this std::vector<Sprite*> mDrawings; std::vector<Sprite*>::iterator it = mDrawings.begin(); This gives
I have a list defined like this: List<MyBean> beanList = getList(); getList() returns type
I have a List defined as: val l = List(1, 2, 3) I want
I have a snippet of code like this: std::list<boost::shared_ptr<Point> > left, right; // ...
I have a List<Person> objects where person is defined like so public Person {

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.