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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:13:34+00:00 2026-06-02T08:13:34+00:00

How does an allocator create and destroy and array, for example int* someInt =

  • 0

How does an allocator create and destroy and array, for example

int* someInt = someAllocator(3);

Where without the allocator it would just be

int* someInt = new int[3];

Where the allocator is responsible for create each element and ensuring the constructor will be called.

How is the internals for an allocator written without the use of new? Could someone provide and example of the function?

I do not want to just use std::vector as I am trying to learn how an allocator will create an array.

  • 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-02T08:13:36+00:00Added an answer on June 2, 2026 at 8:13 am

    The problem of general memory allocation is a surprisingly tricky one. Some consider it solved and some unsolvable 😉 If you are interested in internals, start by taking a look at Doug Lea’s malloc.

    The specialized memory allocators are typically much simpler – they trade the generality (e.g. by making the size fixed) for simplicity and performance. Be careful though, using general memory allocation is usually better than a hodge-podge of special allocators in realistic programs.

    Once a block of memory is allocated through the “magic” of the memory allocator, it can be initialized at container’s pleasure using placement new.

    — EDIT —

    The placement new is not useful for “normal” programming – you’d only need it when implementing your own container to separate memory allocation from object construction. That being said, here is a slightly contrived example for using placement new:

    #include <new> // For placement new.
    #include <cassert>
    #include <iostream>
    
    class A {
    public:
        A(int x) : X(x) {
            std::cout << "A" << std::endl;
        }
        ~A() {
            std::cout << "~A" << std::endl;
        }
        int X;
    };
    
    int main() {
    
        // Allocate a "dummy" block of memory large enough for A.
        // Here, we simply use stack, but this could be returned from some allocator.
        char memory_block[sizeof(A)];
    
        // Construct A in that memory using placement new.
        A* a = new(memory_block) A(33);
    
        // Yup, it really is constructed!
        assert(a->X == 33);
    
        // Destroy the object, wihout freeing the underlying memory
        // (which would be disaster in this case, since it is on stack).
        a->~A();
    
        return 0;
    
    }
    

    This prints:

    A
    ~A
    

    — EDIT 2 —

    OK, here is how you do it for the array:

    int main() {
    
        // Number of objects in the array.
        const size_t count = 3;
    
        // Block of memory big enough to fit 'count' objects.
        char memory_block[sizeof(A) * count];
    
        // To make pointer arithmetic slightly easier.
        A* arr = reinterpret_cast<A*>(memory_block);
    
        // Construct all 3 elements, each with different parameter.
        // We could have just as easily skipped some elements (e.g. if we
        // allocated more memory than is needed to fit the actual objects).
        for (int i = 0; i < count; ++i)
            new(arr + i) A(i * 10);
    
        // Yup, all of them are constructed!
        for (int i = 0; i < count; ++i) {       
            assert(arr[i].X == i * 10);
        }
    
        // Destroy them all, without freeing the memory.
        for (int i = 0; i < count; ++i)
            arr[i].~A();
    
        return 0;
    
    }
    

    BTW, if A had a default constructor, you could try call it on all elements like this…

    new(arr) A[count];
    

    …but this would open a can of worms you really wouldn’t want to deal with.

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

Sidebar

Related Questions

How does std::vector allocate objects? It would seem as if it just uses std::allocator::allocate
If I create a std::vector with the default allocator like this: vector<int> myVec =
I have read that a HTTP server created in node.js does not create new
I was trying to create a 2d array without mentioning the dimensions like as
Does map() iterate through the list like for would? Is there a value in
Unlike new and delete expressions, std::malloc does not call the constructor when memory for
I would like to create a simple file format/DSL which would allow my users
It's allowed to do: public int Age { get; set; } but does the
I have tried to create a template function that does some weighted sampling within
I have a process that must create and close threads on demand. Each thread

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.