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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:16:21+00:00 2026-06-15T03:16:21+00:00

Lets say I wanted to make a vector of objects and another vector of

  • 0

Lets say I wanted to make a vector of objects and another vector of pointers to those objects (I cannot use dynamic memory). The way I would do it is in the following example.

#include <iostream>
#include <vector>

using namespace std;

class Foo {
public:
  int bar;
  Foo(int x) : bar(x) {
  }
};

int main () {
  vector<Foo> foos;
  vector<Foo*> pFoos;
  for (int i = 0; i < 10; i++) {
    Foo foo(i);
    foos.push_back(foo);
    pFoos.push_back(&foos.back());
  }

  for (int i = 0; i < 10; i++) {
    cout << foos[i].bar << endl;
    cout << pFoos[i]->bar << endl;
  }
}

I think this should work because foos stores a copy of the object, and then I store the reference to the copy (because the original foo will be undefined, so I shouldn’t store a reference to that). But this is what I get:

0
36741184
1
0
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

The first to numbers from pFoos are wrong. Furthermore, the large number changes every time. I don’t see anything that would cause this undefined behavior. Could someone tell me what I’m doing wrong?

  • 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-15T03:16:22+00:00Added an answer on June 15, 2026 at 3:16 am

    Adding items to a vector invalidates all previous iterators. Calling push_back on the vector may make the pointers you got from it previously invalid if the vector needs to reallocate its internal storage.

    If you knew that you were never going to grow the vector again then this would work:

    for (int i = 0; i < 10; i++) {
      foos.push_back(Foo(i));
    }
    
    for (int i = 0; i < 10; i++) {
      pFoos.push_back(&foos[i]);
    }
    

    or as commented by rodrigo:

    foos.reserve(10)
    
    for (int i = 0; i < 10; i++) {
      Foo foo(i);
      foos.push_back(foo);
      pFoos.push_back(&foos.back());
    }
    
    for (int i = 0; i < 10; i++) {
      cout << foos[i].bar << endl;
      cout << pFoos[i]->bar << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I making a program and wanted to make an update function... So lets say
Lets say i wanted to add another variable to the database can anybody tell
I don't think this is possible but I wanted to make sure. Lets say
Lets say I wanted to make a post request to update the status of
Lets say i wanted to compare a variable in the sqlite database and if
I wanted to make a dynamic method in which i pass any instance of
To make it easy, lets say I have an arraylist allBooks containing class books
As an example, lets say I wanted to list the frequency of each letter
I wanted to make a special version of shared_ptr that would perform specific operations
Let's say I wanted to make a python script interface with a site like

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.