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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:34:55+00:00 2026-05-16T00:34:55+00:00

Say I have two containers storing pointers to the same objects: std::list<Foo*> fooList; std::vector<Foo*>

  • 0

Say I have two containers storing pointers to the same objects:

std::list<Foo*> fooList;
std::vector<Foo*> fooVec;

Let’s say I remove an object from one of these containers via one if its methods:

std::vector<Foo*>::iterator itr = 
  std::find( fooVec.begin(), fooVec.end(), pToObj );
fooVec.erase( itr );

CppReference says that this calls the object’s destructor. Does this mean that the pointer to the object in fooList is a dangling pointer?

I’d prefer not to use reference counted pointers. How can this problem be handled?

  • 1 1 Answer
  • 2 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-16T00:34:56+00:00Added an answer on May 16, 2026 at 12:34 am

    No.

    When you remove a pointer from a container, all you’ve done is take that pointer value from the container, nothing is deleted. (i.e.: pointers have no destructor.)

    However, it’s dangerous to have pointers of things in containers. Consider:

    std::vector<int*> v;
    v.push_back(new int());
    v.push_back(new int());
    v.push_back(new int());
    

    If you never go through the container and delete each one, you’ve leaked. Worse is it’s not exception safe. You should use a pointer container, which will delete things it points to when they are erased. (And all get erased when the container destructs.)

    In your case, though, since you are sharing a pointer in different places, I can’t see an argument against shared_ptr; that’s exactly what it was made for.

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

Sidebar

Related Questions

I have two objects that are derived from same the base class. Lets say
let's say I have two shapes wrapped in Movieclip container. How can I detect
In tab container let say I have two tabs [Tab1 & Tab2] Tab1 has
Possible stupid question: let's say I have two apps contained within one project. Can
Let say I have are two WPF forms: Form1 and Form2 . Form1 contains
I have two notepads and each notepad contains some data. Let's say Notepad 1
let's say I have two xml strings: String logToSearch = <abc><number>123456789012</number></abc> String logToSearch2 =
Let's say I have two tables one called Customers and one called CustomerAddresses. Then
Let's say I have two lists of strings: a = ['####/boo', '####/baa', '####/bee', '####/bii',
Say I have two lists: List<CanidateSkill> canidateSkills; List<JobDesiredSkill> desiredSkills; class CanidateSkill { public Guid

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.