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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:06:01+00:00 2026-05-26T12:06:01+00:00

Would someone please take a look at the following code fragments (all from one

  • 0

Would someone please take a look at the following code fragments (all from one file) and advise on the problem I will describe at the end?

members:

    std::vector<cHasAMyClass>   collected;
    std::vector<cHasACHasAMyClass>  memberVector;//Actually an array of vectors, but simpler not to be.

constructor of an inner/local class:

    cHasACHasAMyClass(cHasAMyClass *ptrToCHasAMyC, int stuff) :
                masterRef(ptrToCHasAMyC),
                other_stuff(stuff) {}

method I was using:

cHasAMyClass *firstUniqueOccurrence(std::vector<cHasAMyClass> *masterList, myClass *qSq) {
    for (int i = 0; i < (int)masterList->size(); ++i)
        if ((*masterList)[i].myClassPtr == qSq) return &((*masterList)[i]);
    masterList->push_back(cHasAMyClass(qSq));
    return &(masterList->back());
}

block which calls above method:

{
    std::vector<myClass *> allAtLoc;
    if (map->getAllHere(curLoc, &allAtLoc)) {
        for (int i = 0; i < (int)allAtLoc.size(); ++i) {
            int stuff = 42; //Or otherwise
            cHasAMyClass *newContainer = firstUniqueOccurrence(&(collected), allAtLoc[i]);
            memberVector.push_back(cHasACHasAMyClass(newContainer, stuff));
        }
    }
    allAtLoc.clear();
}

Problem is sometime later (ok prolly immediately on leaving the above block) memberVector[0].masterRef appears messed up which I detect (apart from crashing) because its member of type myClass is invalid or null. I thought I had set it to a pointer to persisting object (managed in another data structure, a simple array (/ptr – it’s new/malloc‘d)) but I’m new to using vectors and not sure what sort of indirection I was witnessing.

I have allAtLoc and newContainer going out of scope and though they are but pointers to objects, they have side effects. Incidentally, my collected vector doesn’t contain errors.

This was all working fine before I made the move to std::vectors, I don’t understand how my indirection is wrong, please could someone advise?

  • 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-26T12:06:02+00:00Added an answer on May 26, 2026 at 12:06 pm

    It’s hard to read your code, but it looks like you are taking pointers to elements within a vector. Pointers, iterators and references to a vector are invalidated when its capacity changes. That can happen as a side effect of any function that inserts into the vector, since a new bigger internal array is created into which the current contents are copyed.

    If you want to use pointers to elements in a vector, you have to make sure they won’t be invalidated. If you know the number of elements, you could call reserve prior to inserting any elements.

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

Sidebar

Related Questions

would someone please write this code: this.Loaded += (s, e) => this.loaded = true;
Could someone take a look at this code and find out what's wrong with
Can someone please take a look at the small test case below and tell
Could someone please explain when would I want to use delegation instead of inheritance?
Can someone please shed some light on how I would get this to work:
I would like to access a MySQL database using PHP. Can someone please explain
Can someone please provide some insight into the the following verbosegc lines on a
Can someone please explain the steps I need to take in order to add
Can someone please explain to me how you would create a star schema starting
I'm new to the world of Mac programming. Can someone please tell me what

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.