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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:39:19+00:00 2026-05-24T19:39:19+00:00

I currently have a vector of a class Foo and another vector of pointers

  • 0

I currently have a vector of a class Foo and another vector of pointers that point to a subset of the Foo‘s in the vector. I currently have a function that selects some pointers from the vector<Foo *> and deletes them from the vector<Foo> that actually contains them. Thus, at the outset, the two vectors look like this:

//Foo classes in vector<Foo>
a
b
c
d

and

//Foo pointers in vector<Foo *>
*a
*b
*c
*d

However, I discover that after deleting a Foo, say b, they look like this:

//Foo classes in vector<Foo>
a
c
d

and

//Foo pointers in vector<Foo *>
*a
*c
*d
*d

How come the pointer to c in vector<Foo *> doesn’t stay pointed to c after b gets deleted in vector<Foo>? Is there any way to remedy this?

(I understand that if I delete b, the original pointer to b would be a dangling one; however, since I did not intend to access b after its deletion anyway, I thought it wouldn’t matter and I could go to the next index with the pointer to c)

EDIT:

As requested, the code where the pointer vector is filled:

for(int i = 0; i < a.size(); i++) //a is the vector<Foo>
{
    Foo * thisFoo = &a[i];
    if(someConditionMet)
        b.push_back(thisFoo); //b is the vector<Foo *>
}
  • 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-24T19:39:20+00:00Added an answer on May 24, 2026 at 7:39 pm

    A vector stores its elements in an array. An array is a contiguous sequence of elements.

    If you have a vector containing elements a, b, c, d and remove element b, then elements c and d are moved down one index in the array to fill the hole created by the removed element, leaving an array containing a, c, d.

    The pointers don’t “follow” the elements in the vector as they are moved. If you have a pointer “to c,” that pointer really points to “the element at index 2 in the array.” When you remove b from the array, “the element at index 2” is then d, not c.

    If you want an object to have a fixed address that never changes, you should dynamically allocate it yourself and track it using a smart pointer (like unique_ptr or shared_ptr).

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

Sidebar

Related Questions

I'm currently developing a syntaxic analyser class that needs, at a point of the
I have the following: class Parent { public: virtual bool foo(vector<string> arg1, vector<string> arg2)
I'm trying to create a generic function that removes duplicates from an std::vector. Since
I have a class that requires a boost::variant containing shared pointers to various types
I have a handle-based class that I need to create a vector of. An
I have an std::vector -like class that is compiled with Visual C++ 2008. There's
I currently have a function that is meant to return T (templated function). So
I've got a method that creates some Foo and adds it to a vector
I currently have a class that uses template arguments. I need an array of
I currently have an MS Access application that connects to a PostgreSQL database via

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.