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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:30:49+00:00 2026-05-26T15:30:49+00:00

I have a vector< Object > myvec which I use in my code to

  • 0

I have a vector< Object > myvec which I use in my code to hold a list of objects in memory. I keep a pointer to the current object in that vector in the “normal” C fashion by using

Object* pObj = &myvec[index];

This all works fine if… myvec doesn’t grow big enough that it is moved around during a push_back at which time pObj becomes invalid – vectors guarantee data is sequential, hence they make no effort to keep the vector at the same memory location.

I can reserve enough space for myvec to prevent this, but I dnt’ like that solution.

I could keep the index of the selected myvec position and when I need to use it just access it directly, but it’s a costly modification to my code.

I’m wondering if iterators keep the their references intact as a vector is reallocated/moved and if so can I just replace

Object* pObj = &myvec[index];

by something like

vector<Object>::iterator = myvec.begin()+index;

What are the implication of this?

Is this doable?

What is the standard pattern to save pointers to vector positions?

Cheers

  • 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-26T15:30:50+00:00Added an answer on May 26, 2026 at 3:30 pm

    No… using an iterator you would have the same exact problem. If a vector reallocation is performed then all iterators are invalidated and using them is Undefined Behavior.

    The only solution that is reallocation-resistant with an std::vector is using the integer index.

    Using for example std::list things are different, but also the are different efficiency compromises, so it really depends on what you need to do.

    Another option would be to create your own “smart index” class, that stores a reference to the vector and the index. This way you could keep just passing around one “pointer” (and you could implement pointer semantic for it) but the code wouldn’t suffer from reallocation risks.

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

Sidebar

Related Questions

I have a Vector that holds a number of objects. My code uses a
I have a pointer to a vector of object pointers vector<object*>* vec; Which happens
I have a vector-like class that contains an array of objects of type T
I have created a BST full of WordInfo objects that have a vector to
So I have a container(any kind, probably std::map or std::vector) which contains objects of
I have a set of objects in a Vector from which I'd like to
I have a class that has a vector of another class objects as a
I have a Vector of Object[] type that stores my data. How to print
Would it be smart to have a vector in an object with a list
I have the following code: void Foo() { static std::vector<int>(3); // Vector object is

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.