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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:01:19+00:00 2026-05-23T01:01:19+00:00

I have a function void AddEntity(Entity* addtolist) that pushes elements back onto a vector

  • 0

I have a function void AddEntity(Entity* addtolist) that pushes elements back onto a vector but since the size and capacity are equal when the element is added to the vector, the vector reallocates and the iterator becomes invalid.

Then when I try to increment the iterator I get a crash because of the invalid iterator, since push_back(...) doesn’t return a iterator to the reallocated memory I was wondering how to get around this problem.

Should I just use insert(...) since it does return an iterator, or should I use a pointer that stores the reference to the vector after its reallocated and then have the iterator equal the pointer that points to the reallocated vector?

  • 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-23T01:01:20+00:00Added an answer on May 23, 2026 at 1:01 am
    vector::push_back(const T& x);
    

    Adds a new element at the end of the vector, after its current last element. The content of this new element is initialized to a copy of x.

    This effectively increases the vector size by one, which causes a reallocation of the internal allocated storage if the vector size was equal to the vector capacity before the call. Reallocations invalidate all previously obtained iterators, references and pointers.

    Using an invalidated vector is going to lead you to crashes or undefined behaviors.
    So just get a new iterator by using vector::begin().

    vector<int> myvector;
    
    vector<int>::iterator it;
    it=myvector.begin()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function: void writeResults(FILE* fp, FILE* fpw, Vector w, int size)
I have a function that takes pointer to pointer to struct. struct ABC; void
Suppose I have a function like this: public void AddEntry(Entry entry) { if (entry.Size
I have a function void add_car(char *car) { park.buffer[park.size] = car; park.size ++; }
If I have a function like this: void bla(int size) { while(b){ char tmp[size];
I have a function called: void initializeJSP(string Experiment) And in my MyJSP.h file I
i have a function like void printMe (void *i) { printf(%d, i); } where
I have the following intentionally trivial function: void ReplaceSome(ref string text) { StringBuilder sb
I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a
I have a function in a native DLL defined as follows: #include <string> void

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.