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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:08:56+00:00 2026-06-02T11:08:56+00:00

I have been looking into custom allocators and I quite often see them using

  • 0

I have been looking into custom allocators and I quite often see them using some kind of function to allocate memory. For testing purposes and further educate my self, I tried to make a “simple” example of doing so. However, there is one fundamental thing I am understand on how to do. One of the key differences in malloc vs new is that with new the constructor is called. What if I wanted to write my own allocator that was essentially replacing new, how would I get the constructor to be called when using malloc?

I understand that on classes I can overload new and delete for the class, so I suppose a big part of the question is, how is new calling the objects constructor during allocation? Similarly, I am interested in how delete is calling the destructor.

I created a sample test code that I was hoping to have the SomeClass constructor called during allocation, but I don’t see how.

#include <malloc.h>

void* SomeAllocationFunction(size_t size) {
    return malloc(size);
}

class SomeClass
{
public:
    SomeClass() {
        int con = 1000;
    }

    ~SomeClass() {
        int des = 80;
    }
};

int main(void){
    SomeClass* t = (SomeClass*)SomeAllocationFunction(sizeof(SomeClass));
    return 0;
}

(As a note, I know I can just use new. However, for the purposes of learning I am trying to create a custom allocator that does not just call new or placement new).

  • 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-02T11:08:59+00:00Added an answer on June 2, 2026 at 11:08 am

    In essence, when you use a new expression like: T *t = new T;, it’s roughly equivalent to:

    void *temp = operator new(sizeof(T));
    T *t = new(temp) T;
    

    So, first it allocates some raw memory using the allocation function, then it constructs an object in that memory. Likewise, when you use a delete expression like: delete t;, it’s roughly equivalent to:

    t->~T();
    operator delete(t);
    

    So, if you overload new and delete for a particular class:

    class T { 
        int data; 
    public:
        // I've made these static explicitly, but they'll be static even if you don't.
        static void *operator new(size_t size) { 
            return malloc(size);
        }
        static void operator delete(void *block) { 
            free(block);
        }
    };
    

    Then when you use a new expression, it’ll invoke the class’ operator new to allocate the memory, and that will call malloc, so T *t = new T(); will end up allocating memory via malloc (and likewise, when you delete it, it’ll use operator delete, which will call free).

    At least as the term is normally used, an Allocator is quite similar, except that it’s used by a container instead of other code. It also encapsulates the allocation function and deletion function into a class, so when you pass one to the container, you only have to pass one object, and there’s little chance of an allocation and delete function getting mismatched.

    Ignoring, for the moment, the details about what names are used for things, the Allocator class in the standard library mostly does the same, so with a little renaming of the functions in the T class above, you’d be about half done writing a standard allocator. To go with the allocation and deletion, it has a function to rebind some memory (change a block of memory to another type), create an object in place (basically just a wrapper around a placement new) and destroy an object (again, trivial wrapper around destructor invocation). Of course, it uses operator new and operator delete instead of malloc and free like I’ve used above.

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

Sidebar

Related Questions

I have been looking into creating a custom mySQL Point Of Sales system so
I have been looking into using the Entity Framework in my C# game server
I have been looking into CruiseControl configuration recently (I'm a complete CC noob) and
I have been looking into different systems for creating a fast cache in a
I have been looking into MVVM design patterns with WPF for a project. I
I have been looking into HTML5 manifest but I am unclear as to whether
I have been looking into coloring objects like ellipses with code such as SolidBrush
I have been looking into packages that would enable me to have tabs in
I have been looking into integrating testing into my app based on RequireJS. I
I have been looking into Spring Social Facebook's publish(objectId, connectionName, data) API , but

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.