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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:12:38+00:00 2026-05-26T08:12:38+00:00

This question is extension of the question I want to understand how the elements

  • 0

This question is extension of the question I want to understand how the elements are inserted into the STL Container.

Suppose I have A object;, which I want to insert into any of the STL Container, I understand that there is concept of allocators which handles the memory. But I fail to understand that how the actual object is copied into STL memory. So my object is stored on the stack when I call Container.insert how does STL create copy of this object and stored this objects into its memory.

Any equivalent C++ code would be helpful which simulates the same.

  • 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-26T08:12:39+00:00Added an answer on May 26, 2026 at 8:12 am

    The approach is not that complicated. Basically the container will obtain memory from the allocator and then perform copy-construction (with placement-new over that memory). The easier container to see is vector:

    void push_back( T const & value ) {
       ensure_enough_capacity();
       new (end_ptr++) T( value );
    }
    

    Where ensure_enough_capacity() determines whether the vector has to grow and does it, that is, it will end up calling the allocator if size()==capacity() when push_back is called.

    The next level of complexity is a list, where each node is allocated on its own, and there is some extra information that the library has to manage. In that case the code would look similar to:

    void push_back( T const& value ) {
        node* n = allocator::allocate( sizeof(node) );
        new (n) node( value, x, y );
    }
    

    Where x and y are the appropriate pointers to initialize the node’s next and last pointers (usually would be a pointer to the last node for last and a pointer to a sentry node –invalid beyond the end– for next), and assuming that this particular constructor will copy-construct the value and then fix all referred pointers.

    Ordered associative containers have the extra level of complexity of managing the balanced tree, but the approach is the same: allocate a block big enough to hold the value and the extra information, and then use placement-new to build the node. The rest are details of the data structure.

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

Sidebar

Related Questions

My question is an extension of this question I want to link against 2
This is an extension of this question. I want to include (not literalinclude) a
I have an extension to this question: Vb6 "Tag" property equivalent in ASP.Net? The
As an extension of this question , I'm trying to insert Javascript to a
This is an extension of this question . I have searched on the web
This question is an extension to the one raised here: Using factory_girl in Rails
This question already has an answer here: What are Extension Methods? 5 answers Usage
As an extension to this question here Linking JavaScript Libraries in User Controls I
This is an extension for this question asked an hour ago. We cannot modify
This is an extension / next step of this question I asked a few

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.