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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:55:59+00:00 2026-05-31T14:55:59+00:00

I have a problem while working with vectors of a class. I have tried

  • 0

I have a problem while working with vectors of a class. I have tried whittling away at the code as much as possible, leaving me with the code below. Yet, after doing this, I still cannot figure out why my program is crashing. I come from a world of VBA and my C++ debugging skills are poor to say the least and I apologize for that up front. Any guidance to improve my ability here will be gladly accepted.

My class:

class Tester {
public:

    int varA;
    int varB;

    Tester() {
        varA = 1;
        varB = 1;
    }

    Tester(Tester& P1, Tester& P2) {
        varA = P1.varA + P2.varA;
        varB = P1.varB + P2.varB;
    }

    Tester(const Tester &Source) {
        varA = Source.varA;
        varB = Source.varB;
    }

};

My test mains:
Does not work:

int main() {
std::vector < Tester > BreakIt;

    for (int i = 0; i < 2500; i++) {
        Tester newTester;
        BreakIt.push_back(newTester);
        Tester& tempTester = BreakIt.at(0);
        for (int j = 0; j < 4; j++) {
            BreakIt.push_back(Tester(newTester, tempTester)); //This is where it crashes.
        }
    }

return 0;
}

Does work:

int main() {
std::vector < Tester > BreakIt;

    Tester newTester;
    BreakIt.push_back(newTester);

    for (int i = 0; i < 2500; i++) {
        Tester& tempTester = BreakIt.at(0);
        for (int j = 0; j < 4; j++) {
            BreakIt.push_back(Tester(newTester, tempTester));
        }
    }

return 0;
}

Does work:

int main() {
std::vector < Tester > BreakIt;

    for (int i = 0; i < 2500; i++) {
        Tester newTester;
        BreakIt.push_back(newTester);
        Tester& tempTester = BreakIt.at(0);
    }

return 0;
}

The push_back() line that breaks does run a few times before the crash. It seems that when I change certain things around and recompile, the point at which it crashes changes (i.e. 20 times through the main loop vs. 175 times vs. 1000 times). However, once I’ve compiled, it will typically run through the the same iteration each time before crashing.

I suspect that this is some kind of ‘undefined behavior’ but from where I’m not sure.

Can someone please help me identify why BreakIt.push_back(Tester(newTester, tempTester)); does not work where I want it to?

  • 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-31T14:56:01+00:00Added an answer on May 31, 2026 at 2:56 pm

    You’re right, there is undefined behaviour.

    You’re taking a reference to BreakIt.at(0); however this object is no longer guaranteed to exist after you’ve done a push_back, because if the vector grows, all of the contents are typically copied, and the originals deleted.

    From http://www.cplusplus.com/reference/stl/vector/push_back/ (not the world’s best reference):

    Reallocations invalidate all previously obtained iterators, references and pointers.

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

Sidebar

Related Questions

I have been working on this problem for a while now. I am trying
I have a problem while pasting my contents (or text) generated by Java code
I have come across an annoying problem while writing some PHP4 code. I renamed
I have a strange problem while working with CCSprite subclass Creature. Lets,my object is
I am working with directX 9. I have a problem while rendering triangles using
I have hit a problem while working with the Units of Measurements functionality in
I just figured out that I have a problem in IE while working with
I have a problem while comparing the values in two vectors. Following is the
I have a small problem while working on software for a Surface: I have
I encountered a code design problem while working on a small application. In terms

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.