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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:35:42+00:00 2026-05-30T04:35:42+00:00

I am using a std::vector to store an array of objects that are referenced

  • 0

I am using a std::vector to store an array of objects that are referenced from outside of the vector by other objects. I drew a diagram to explain more clearly:

std::vector with objects being referenced

I am storing objects rather than pointers for performance reasons. These objects are sorted every frame of my game, so I want the array to have good cache properties.

Of course, whenever objects are added to the vector, there is a chance that the array will be reseated. In that case my references are invalidated and need to be updated. Right now, to detect the reseating, I am using the following method:

size_t old_capacity = v.capacity();

// Do stuff that could change the vector's size
v.push_back(a);
v.push_back(b);
v.push_back(c);

if (old_capacity != v.capacity()) {
    update_references();
}

My questions are:

  • Is this the best way to detect that the vector has reseated its array?
  • Do I also need to check for reseating after performing pop_back?
  • 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-30T04:35:43+00:00Added an answer on May 30, 2026 at 4:35 am

    In my mind, the best method would be the following: Just use the pointer to the head of the vector. Not all reallocations will cause the vector to move.

    However, I more or less agree with the comments about references into your vector. Also, you could use a std::list which wouldn’t have the problems with reallocations.

    std::vector<int> v;
    
    void *old_location = (void *) &(v.front());
    
    v.push_back(3);
    v.push_back(3);
    v.push_back(3);
    
    if (old_location != &(v.front()))
        update_references();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a datatype of std::vector<std::vector<T> > to store a 2D matrix/array. I
I am using a std::vector to store some strings, later I try to std::find
I'm using a std::deque to store a fairly large number of objects. If I
I'm using the 'using' declaration in C++ to add std::string and std::vector to the
When using the std::for_each, class A; vector<A*> VectorOfAPointers; std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo)); If we have
#include <iostream> using namespace std; // This first class contains a vector and a
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {

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.