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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:17:11+00:00 2026-05-27T01:17:11+00:00

When vector needs more memory it will reallocate memory somewhere, I don’t know where

  • 0

When vector needs more memory it will reallocate memory somewhere, I don’t know where yet! and then pointers become invalid, is there any good explanation on this?

I mean where they go, what happen to my containers? ( not linked list ones )

  • 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-27T01:17:12+00:00Added an answer on May 27, 2026 at 1:17 am

    Short answer: Everything will be fine. Don’t worry about this and get back to work.

    Medium answer: Adding elements to or removing them from a vector invalidates all iterators and references/pointers (possibly with the exception of removing from the back). Simple as that. Don’t refer to any old iterators and obtain new ones after such an operation. Example:

    std::vector<int> v = get_vector();
    
    int & a = v[6];
    int * b = &v[7];
    std::vector<int>::iterator c = v.begin();
    std::advance(it, 8);
    
    v.resize(100);
    

    Now a, b and c are all invalid: You cannot use a, and you cannot dereference b or c.

    Long answer: The vector keeps track of dynamic memory. When the memory is exhausted, it allocates a new, larger chunk elsewhere and copies (or moves) all the old elements over (and then frees up the old memory, destroying the old objects). Memory allocation and deallocation is done by the allocator (typically std::allocator<T>), which in turn usually invokes ::operator new() to fetch memory, which in turn usually calls malloc(). Details may vary and depend on your platform. In any event, any previously held references, pointers or iterators are no longer valid (presumably because they refer to the now-freed memory, though it’s not specified in the standard why they’re invalid).

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

Sidebar

Related Questions

Valgrind detects an invalid read error I don't know how to fix or to
I have a vector of pointers to a class. I need to call their
We all know that RAW pointers need to be wrapped in some form of
Description When allocating and deallocating randomly sized memory chunks with 4 or more threads
hi i'm doing a program that needs high performance of handling vector elements vector<Class_A>
I'm looking at building a wrapper for the C++ STL container vector (there will
Question of the century? I basically want to know which would be more efficient
I'm pretty new to C++, but I know you can't just use memory willy
I need to take a C++ vector with potentially a lot of elements, erase
We need to decide between Support Vector Machines and Fast Artificial Neural Network for

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.