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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:56:38+00:00 2026-05-27T06:56:38+00:00

I have a problem whereby a vector of pointers does not seem to be

  • 0

I have a problem whereby a vector of pointers does not seem to be behaving in the polymorphic way I expected it to. Stranger is the fact that it works on every other object it points to.

shotgunNodes and rifleNodes both contain two integers which represent the appropriate location in a level to place the pickups I’m generating.

pickups is a vector of kpointers (parent class) and ShotgunPU and RiflePU are derived classes. I’ll post the headers and source below.

The strange problem is that if I break the code, one loop at a time, everything seems to be going fine. The first gun pointer is set to point at a rifle object. Fine. Then the next gun pointer is set to point at the next rifle object, at which point for some bizarre reason the previous pointer in the vector, that should point to the first rifle is cut down and now only points to the gun part of the rifle object, so I lose all of the derived functionality on it.

By the end of the loops, I have a vector of guns pointing to Gun Rifle Gun Shotgun where it should be Rifle Rifle Shotgun Shotgun.

int j = 0;

for (vec_i_sz i = 0; i < rifleNodes.size(); i++, j++)
{
    riflePickups.push_back(RiflePU(agents, mesh));
    riflePickups[i].Position(nodes[rifleNodes[i]].Position());

    pickups.push_back(&riflePickups[i]);//point the pickup pointer to the rifle pickup
    pickups[j]->Index(j);
    pickups[j]->NodeIndex(rifleNodes[i]);
}

for (vec_i_sz i = 0; i < shotgunNodes.size(); i++, j++)
{
    shotgunPickups.push_back(ShotgunPU(agents, mesh));
    shotgunPickups[i].Position(nodes[shotgunNodes[i]].Position());

    pickups.push_back(&shotgunPickups[i]);
    pickups[j]->Index(j);
    pickups[j]->NodeIndex(shotgunNodes[i]);
}

It’s probably also worth noting that this code (obviously the translated version, but in the same structure) works in the C# version of the program. So I doubt it’s the structure of the code. Is there something I don’t know about the way pointers work?

This is how the vectors are declared in the header where they are used:

std::vector<Pickup*> pickups;
std::vector<RiflePU> riflePickups;
std::vector<ShotgunPU> shotgunPickups;

I just noticed by writing this that they are not actually initialized in the initializer list before the loops run (in the constructor’s body), but shouldn’t this be OK because they are class types?

If they are being initialised in the list, it’s still happening.

pickups(std::vector<Pickup*>()),
riflePickups(std::vector<RiflePU>()),
shotgunPickups(std::vector<ShotgunPU>())
  • 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-27T06:56:38+00:00Added an answer on May 27, 2026 at 6:56 am

    When you add items to a vector, push_back can invalidate all pointers into the container when it needs to resize its underlying buffer. Each time this happens, the pointers that you have already stored in pick_ups become garbage.

    1) You can reserve memory for all items in riflePickups and shotgunPickups upfront. This will ensure that those vectors won’t be resized while that many items are added, and addresses of items will remain the same.

    2) You can store the pointers in pick_ups after you have created all the items in both vectors, not while those vectors have not yet reached their final size.

    3) You can use std::list over vector for riflePickups and shotgunPickups. In a list, pointers to items are only invalidated if said item is erased.


    Eventually, do you really need any of this? Perhaps you could just store (preferably smart) pointers to dynamically allocated objects in just one vector.

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

Sidebar

Related Questions

We currently have a problem whereby on our stage servers, links that exist on
I have a strange problem whereby the ValidationSummary is not being displayed. However, the
I have a seemingly simple problem whereby I wish to reconcile two lists so
I have encountered a problem twice now whereby a producer thread produces N work
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with fancybox. I want to write a function that will run
I have a problem whereby I have set the body font-size to 11px, but
iPhone Safari seems to have some sort of problem whereby a page with css
I have an odd problem. I have a unit test that keeps getting stuck
I have the problem whereby I can't keep my sub menu visible. When I

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.