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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:23:57+00:00 2026-05-15T07:23:57+00:00

I am thinking of how I can implement std::vector from the ground up. How

  • 0

I am thinking of how I can implement std::vector from the ground up.

How does it resize the vector?

realloc only seems to work for plain old stucts, or am I wrong?

  • 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-15T07:23:58+00:00Added an answer on May 15, 2026 at 7:23 am

    it is a simple templated class which wraps a native array. It does not use malloc/realloc. Instead, it uses the passed allocator (which by default is std::allocator).

    Resizing is done by allocating a new array and copy constructing each element in the new array from the old one (this way it is safe for non-POD objects). To avoid frequent allocations, often they follow a non-linear growth pattern.

    UPDATE: in C++11, the elements will be moved instead of copy constructed if it is possible for the stored type.

    In addition to this, it will need to store the current “size” and “capacity”. Size is how many elements are actually in the vector. Capacity is how many could be in the vector.

    So as a starting point a vector will need to look somewhat like this:

    template <class T, class A = std::allocator<T> >
    class vector {
    public:
        // public member functions
    private:
        T*                    data_;
        typename A::size_type capacity_;
        typename A::size_type size_;
        A                     allocator_;
    };
    

    The other common implementation is to store pointers to the different parts of the array. This cheapens the cost of end() (which no longer needs an addition) ever so slightly at the expense of a marginally more expensive size() call (which now needs a subtraction). In which case it could look like this:

    template <class T, class A = std::allocator<T> >
    class vector {
    public:
        // public member functions
    private:
        T* data_;         // points to first element
        T* end_capacity_; // points to one past internal storage
        T* end_;          // points to one past last element
        A  allocator_;
    };
    

    I believe gcc’s libstdc++ uses the latter approach, but both approaches are equally valid and conforming.

    NOTE: This is ignoring a common optimization where the empty base class optimization is used for the allocator. I think that is a quality of implementation detail, and not a matter of correctness.

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

Sidebar

Related Questions

How can I implement a foreign key in SQLite? I was thinking something like
How can I implement the fairly new Voice Actions from Google, in Android? I'm
How can I implement the fairly new Voice Actions from Google, in Android? I'm
I'm thinking about how can I implement onfocus and onblur effects in my input
I've written this class that returns feed updates, but am thinking it can be
i was thinking about designing a service that can provide my subscribers feed updates
Can any one guide how to search number with wildcard on thinking sphinx... I
I've been thinking about this and I just can't think of a way to
Can anyone explain what might be causing this error. Im thinking its the quotes.
I'd like to begin thinking about how I can scale up my algorithms that

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.