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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:46:16+00:00 2026-06-02T22:46:16+00:00

How does std::vector allocate objects? It would seem as if it just uses std::allocator::allocate

  • 0

How does std::vector allocate objects? It would seem as if it just uses std::allocator::allocate to create a block of memory, but then never calls std::allocate::construct. Is this true? Does std::vector only allocate memory and never construct the objects as memory allocation?

What if there is no default constructor? How is the constructor called when there is no default constructor on the object? What if there is more than one parameter?

For example, with this code there is no default constructor and std::allocator allows it.

#include <vector>
using namespace std;

class A{
protected:
    int m;
public:
    explicit A(int a) : m(a) { }
};

int main(){
    vector<A> test;
    return 0;
}
  • 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-06-02T22:46:17+00:00Added an answer on June 2, 2026 at 10:46 pm

    This has rather changed since C++11.

    In C++03, construct could only perform copy-construction in-place.

    However, note that std::vector in particular is an array of objects, but there is a distinct size and capacity. That is, there can be more empty elements beyond the end of the part of the array that contains useful data.

    That is why the standard library’s allocator has a separation between “construction” and “memory allocation.” The allocator does do both, but not at the same time. This allows std::vector to allocate more memory than it uses. When you add new elements, it doesn’t necessarily have to allocate more memory; it can just use the spare memory it has left over via a call to allocator::construct.

    Also, note that all of the C++03 functions that add elements to std::vector take an element as a parameter. push_back, insert, even the sized constructor takes as a value as an argument. Yes, it’s a default parameter, but it still takes a value as an element. This element is copied into the vector, using a call to the allocator’s construct method which takes a copy.

    In C++11, standard containers are required to use the allocator_traits<>::construct function. This is a varadic function that forwards its parameters to the actual construct. This traits function will (by default. It can be specialized) call the allocator::construct method if that call is well-formed. If it isn’t, it will try placement new.

    This allows for the new emplace functions to work.

    But yes, the objects contained in standard library containers are in fact constructed objects. Even if the allocator’s construct method is not called.

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

Sidebar

Related Questions

What does the default allocator do when a std::vector is resized (via either reserve()
How does stl call the destructors of objects, as in std::vector::erase or std::vector::pop_back?
Possible Duplicate: Does std::vector.clear() do delete (free memory) on each element? I have a
If std::vector and friends are self resizing, does that mean if I declare a
When I call std::vector::reserve when the identifier is of type std::vector<Foo*> reserve(...) does nothing:
Possible Duplicate: Are std::vector elements guaranteed to be contiguous? does std::vector always contain the
A different question inspired the following thought: Does std::vector<T> have to move all the
I would like to be able to convert between std::vector and its underlying C
Where does the C++ standard declare that the pair of iterators passed to std::vector::insert
std::vector::erase() does not accept reverse iterator. Is there any way to call this method

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.