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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:12:21+00:00 2026-05-25T15:12:21+00:00

I am currently really stuck on how to solve the following problem: struct node

  • 0

I am currently really stuck on how to solve the following problem:

struct node {
    int a;
    node* b;
}

I will have two root nodes without parents and then to more nodes pointing to their parents

vector<node> IholdAllTheNodes;

node noparent1; <--- these get returned from a method
node noparent2;

IholdAllTheNodes.pushback(noparent1);
IholdAllTheNodes.pushback(noparent2);

node withparent1; <---- i am happily points to noparent1
node withparent2; <-----i am happily points to noparent2

No problems up to here all working awesomely

IholdAllTheNodes.pushback(withparent1) <<<< oops this causes all the pointers to move.

now the withparent nodes are no longer pointing to their parents because I have moved them.

My question is simply how do I add more nodes to my vector of nodes without moving the pointers of the original nodes. (I cant get a fixed size for the vector)

if anyone has the time to explain, why even though pushback is adding to the end of the vector list is the location of the previous information changing?

  • 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-25T15:12:22+00:00Added an answer on May 25, 2026 at 3:12 pm

    Your problem is that you store nodes by value in vector. They are copied, potentially many times. Each time a node will be placed in different memory address. As result you’ll receive different pointers to it, and old pointers will point to invalid location.

    As other answers suggested, use any kind of pointers to store your nodes in vector. I would recommend boost::ptr_vector<node> or std::vector<boost::shared_ptr<node> >

    EDIT:

    Just to disagree with reserve recommendations:

    It’s dangerous. You need to put large bold warnings in all possible places so people who’ll modify this code in the future cannot omit it. Because when you’ll add more than MAX_SIZE elements to your vector, your space ship will end burning on Sun. In this case I would recommend to use boost::array since it’s fixed size (so making your assumption more obivious) and catches buffer overflow at least in Debug builds

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

Sidebar

Related Questions

I have this problem when I deploy, I really don't know how to solve
I am currently stuck at a really strange problem with GDI and timers. First
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
Really hope someone can help me as I'm a bit stuck :S I have
I'm stuck on this. Currently I'm using: FILE *a = fopen(sample.txt, r); int n;
I have currently moved my blogengine over from Linq2Sql to NHIbernate. I'm stuck at
Currently having this really odd problem with my layout IE7. I'm not very experienced
I have been trying these for past two days and getting stuck at same
I have been stuck with this problem for a while now, and I just
I'm currently writing a program to generate really enormous (65536x65536 pixels and above) Mandelbrot

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.