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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:20:32+00:00 2026-06-17T09:20:32+00:00

This may well be a really silly/basic question, but I can’t really figure it

  • 0

This may well be a really silly/basic question, but I can’t really figure it out. I’ll tell you what I think – please correct me if/where I’m wrong.

When we’re using STL containers to store raw pointers, we have to take care of cleaning up after them:

std::list<animal*> alist;
animal *a = new animal();
alist.push_back(a);
...
animal *b = alist.front();
alist.pop_front();
//do stuff with b
delete b;

What happens if we store objects? It is my understanding that if a container full of objects goes out of scope, all the objects inside it are destroyed. Correct?

But what if we remove an object from a container, using std::list<T>.pop_front() for example?

std::list<animal> alist;
{
  animal ani();
  //ani is inserted at the end of the list (it IS ani rather than a copy 
  //since push_back accepts const T&)
  alist.push_back(ani);
} //nothing is deallocated/destroyed here
...
{
  animal b = alist.front(); //b is now a copy(?!) of the front element
                            //created via copy constructor
  alist.pop_front(); //the front element of the list is 
                     //destroyed/destructor is called 
  //do stuff with b
} //b goes out of scope
  • 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-17T09:20:33+00:00Added an answer on June 17, 2026 at 9:20 am

    When you store something in a container, whatever you pass is copied into the container1. You still own the original object2; the container owns its copy of the object. It’s true that push_back (for one example) takes its parameter by reference. This means when you pass your object to be pushed into the container, no copy is needed for use as the parameter itself, but what’s put into the container is still a copy of what you told it to push. Passing by reference means only one copy is needed; if it was passed by value, (at least in C++98/03) that would have resulted in two copies: one copy from your object to the function argument, then another copy from the function argument into the container. Passing by reference allows a single copy directly from your original object directly into the container.

    Likewise, when you get an object from the container, you’re getting a copy of what was in the container. The container still has its object, and you have your object. Each of these has its own lifetime.

    When you erase or pop an object from a container, that object is removed from the container and destroyed — if it’s something that has a destructor, the destructor will be called to destroy it.

    The objects in a container will be destroyed when the container itself is destroyed. Your object will be destroyed whenever it happens to be — the fact that it came from a container has no effect on that. If it’s a local variable, it’ll be destroyed when it goes out of scope. If it’s a global, it’ll last ’til the end of time (for the program). If you return it from a function and that’s used to initialize a reference, the normal rule for extending its lifetime there will be observed. Bottom line: at that point, it’s just another object, that’ll have a lifetime just like any other object defined in the same way.

    This can get a little…fuzzy when/if you store pointers (raw or smart) in a container. All of the above actually remains true, but the object being copied/stored is a pointer. Unless you use some container that’s “aware” that you’re storing pointers (e.g., Boost ptr_vector) it’s just dealing with a pointer, and “ignoring” the fact that somewhere out there is an object that the pointer refers to.


    1. In C++11, you can use, for example, emplace_back to construct an object in place in the container, but the same basic idea applies — the container has an object that it owns, and you never touch directly.
    2. Here I’m using “object” more like a non-programmer would — just to mean “some thing”, not necessarily an instance of a class. It may not even be an “object” in the sense that C uses the word — it could be a pure rvalue (e.g., push_back(10)).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question may sound a bit stupid, but I couldn't figure out, how to
I know this question may well be the silliest question you've heard today, but
Well, I'm learning Scala so this question may be too basic for most people.
This may be opinionated or whatever, but I think its a fair question to
Well I know that this question may be asked many times but certainly I've
This may well be simple, but I'm new to Drupal. The organization I work
This may well be a dumb question and if this has already been answered
This may be a stupid question but I have a code with the following
Ok, this may seem like a dupe question, but if you bear with me
So this may be vauge but hold on and see if you can understand

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.