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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:29:39+00:00 2026-05-11T11:29:39+00:00

How does std::vector implement the management of the changing number of elements: Does it

  • 0

How does std::vector implement the management of the changing number of elements: Does it use realloc() function, or does it use a linked list?

Thanks.

  • 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. 2026-05-11T11:29:40+00:00Added an answer on May 11, 2026 at 11:29 am

    It uses the allocator that was given to it as the second template parameter. Like this then. Say it is in push_back, let t be the object to be pushed:

    ... if(_size == _capacity) { // size is never greater than capacity     // reallocate     T * _begin1 = alloc.allocate(_capacity * 2, 0);     size_type _capacity1 = _capacity * 2;      // copy construct items (copy over from old location).     for(size_type i=0; i<_size; i++)         alloc.construct(_begin1 + i, *(_begin + i));     alloc.construct(_begin1 + _size, t);      // destruct old ones. dtors are not allowed to throw here.      // if they do, behavior is undefined (17.4.3.6/2)     for(size_type i=0;i<_size; i++)         alloc.destroy(_begin + i);     alloc.deallocate(_begin, _capacity);      // set new stuff, after everything worked out nicely     _begin = _begin1;     _capacity = _capacity1; } else { // size less than capacity     // tell the allocator to allocate an object at the right     // memory place previously allocated     alloc.construct(_begin + _size, t); } _size++; // now, we have one more item in us ... 

    Something like that. The allocator will care about allocating memory. It keeps the steps of allocating memory and constructing object into that memory apart, so it can preallocate memory, but not yet call constructors. During reallocate, the vector has to take care about exceptions being thrown by copy constructors, which complicates the matter somewhat. The above is just some pseudo code snippet – not real code and probably contains many bugs. If the size gets above the capacity, it asks the allocator to allocate a new greater block of memory, if not then it just constructs at the previously allocated space.

    The exact semantics of this depend on the allocator. If it is the standard allocator, construct will do

    new ((void*)(_start + n)) T(t); // known as 'placement new' 

    And the allocate allocate will just get memory from ::operator new. destroy would call the destructor

    (_start + n)->~T(); 

    All that is abstracted behind the allocator and the vector just uses it. A stack or pooling allocator could work completely different. Some key points about vector that are important

    • After a call to reserve(N), you can have up to N items inserted into your vector without risking a reallocation. Until then, that is as long as size() <= capacity(), references and iterators to elements of it remain valid.
    • Vector’s storage is contiguous. You can treat &v[0] as a buffer containing as many elements you have currently in your vector.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Here are some general guidelines I try to abide by,… May 11, 2026 at 12:55 pm
  • added an answer You can't, anti-aliased edges require per-pixel alpha channel (what AllowTransparency=true… May 11, 2026 at 12:54 pm
  • added an answer You don't, easily at least... the SELECT is embedded into… May 11, 2026 at 12:54 pm

Related Questions

How does Google Chrome command and control multiple cross platform processes and provide a
How does a virtual machine generate native machine code on the fly and execute
How does one go about authoring a Regular Expression that matches against all strings
How does one go about referencing a class's static properties in xaml? In other
How does the new Microsoft asp.net mvc implementation handle partitioning your application - for
std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a
I've stumbled across this great post about validating parameters in C#, and now I
I have a vector-like class that contains an array of objects of type T
I have an input file that I want to sort based on timestamp which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.