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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:02:27+00:00 2026-05-12T22:02:27+00:00

I was wondering if there is any difference in performance when you compare/contrast A)

  • 0

I was wondering if there is any difference in performance when you compare/contrast

A) Allocating objects on the heap, putting pointers to those objects in a container, operating on the container elsewhere in the code

Ex:

std::list<SomeObject*> someList;

// Somewhere else in the code
SomeObject* foo = new SomeObject(param1, param2);
someList.push_back(foo);

// Somewhere else in the code
while (itr != someList.end())
{
    (*itr)->DoStuff();
    //...
}

B) Creating an object, putting it in a container, operating on that container elsewhere in the code

Ex:

std::list<SomeObject> someList;

// Somewhere else in the code
SomeObject newObject(param1, param2);
someList.push_back(newObject);

// Somewhere else in the code
while (itr != someList.end())
{
    itr->DoStuff();
    ...
}

Assuming the pointers are all deallocated correctly and everything works fine, my question is…

If there is a difference, what would yield better performance, and how great would the difference be?

  • 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-12T22:02:27+00:00Added an answer on May 12, 2026 at 10:02 pm

    There is a performance hit when inserting objects instead of pointers to objects.

    std::list as well as other std containers make a copy of the parameter that you store (for std::map both key and value is copied).

    As your someList is a std::list the following line copies your object:

    Foo foo;
    someList.push_back(foo);           // copy foo object
    

    It will get copied again when you retrieve it from list. So you are making of copies of the whole object compared to making copies of pointer when using:

    Foo * foo = new Foo();
    someList.push_back(foo);             // copy of foo*
    

    You can double check by inserting print statements into Foo’s constructor, destructor, copy constructor.

    EDIT: As mentioned in comments, pop_front does not return anything. You usually get reference to front element with front then you pop_front to remove the element from list:

    Foo * fooB = someList.front();    // copy of foo*
    someList.pop_front();
    

    OR

    Foo fooB = someList.front();  // front() returns reference to element but if you
    someList.pop_front();         // are going to pop it from list you need to keep a
                                  // copy so Foo fooB = someList.front() makes a copy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there is any difference in performance (or any other important
I was wondering is there any difference in terms of performance between the two
I was wondering if there is any difference in performance/memory use between movieclip symbol
I was just wondering if there is any difference between the two different new
I'm (finally) starting to learn regex, and I'm wondering if there's any notable difference
Is there any difference in performance (in terms of inserting/updating & querying) a table
So I was wondering if there are any major differences between the various implementations
Wondering if there is any Text to Speech software available as a plug in
Wondering if there's any not-too-hard way to edit non-form text in html 4. I
Just wondering if there is any way (in C) to get the contents of

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.