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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:49:33+00:00 2026-06-02T23:49:33+00:00

I have a #define to my new macro, to use my own allocator, such

  • 0

I have a #define to my new macro, to use my own allocator, such as MYNEW(Type, Allocator) where I will go and allocate some raw memory using malloc, then later use placement new to allocate the type on the raw memory such as

#define MYNEW(Type, Allocator) Allocator->Alloc<Type>(sizeof(Type));`
template<typename Type>
Type* Alloc(unsigned int size) // Allocator::Alloc<Type>
{
    return (Type*)new(malloc(reportedSize)) Type;
}

However, I am running into issues when there is no default constructor for Type. One scenario I tried was doing something such as MYNEW_1(Type, Allocator, ConVar1) where ConVar1 would be passed such as

#define MYNEW_1(Type, Allocator, ConVar1) Allocator->Alloc<Type>(sizeof(Type), ConVar1);`
template<typename Type, typename ConVarType>
Type* Alloc(unsigned int size, ConVarType Convar1) // Allocator::Alloc<Type>
{
    return (Type*)new(malloc(reportedSize)) Type(Convar1);
}

The problem with this approach is that for my custom Vector, I am also using MYNEW to allocation memory. However, for some Type‘s used for my Vector, there is no default constructor and I can’t tell how many variables the constructor may require.

Does anyone have any insight into how this problem could be solved? (Without of course saying using std:: types instead of my own. I am doing this to learn more). I do not wish to just overload operator new as I have memory tracking in there as I do not wish to have the memory tracked twice (there is more internal tracking to Alloc, but what I have shown is the simplified example) and would rather just use malloc.

  • 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-02T23:49:36+00:00Added an answer on June 2, 2026 at 11:49 pm

    It appears that you’re not only doing your own custom allocator, but that you’re also not necessarily trying to define an STL-compatible allocator. However, I think it’s useful to recognize that for all their faults, STL allocators were designed to handle this case. Allocation is separate from construction:

    Notice that [allocator::construct] does not allocate space for the element, it should already be available at p (see member allocate to allocate space).

    It is equivalent to [call to placement new that copies an existing object].

    Generally speaking, I believe you have two options: either (1) overload operator new to handle allocating memory and let the system handle the construction, or (2) make your allocator interface more like the STL allocator interface.


    "Overloading operator new" is often used to mean two things: (1) replacing it, or (2) adding a new version of new. When I originally answered, I was using the first meaning (which really should be called "replacing operator new" or "overriding operator new"); but thinking more about this, I’ve determined that truly overloading operator new would meet your requirements.

    I should mention that the syntax to call an overloaded operator delete is so bad that many people avoid this technique for that reason alone. Instead of overloading delete, you can create a function (say, deallocate) and use it.

    The value of this approach is that you follow the language standard of separating memory allocation from object construction, and you leave it up to the compiler to take care of the object construction for you. You don’t have to worry about forwarding operators, or about classes without default constructors, or any of those issues.

    Overloading operator new/operator delete (of course, I’m relying on you to flesh out track and release):

    #include <new>
    #include <cstdlib>
    
    struct Allocator {
        void track(void* p, const void* container) const;
        void release(void* p, const void* container) const;
    };
    
    void* operator new (size_t size, const Allocator& alloc, const void* container)
    {
        void* allocated_memory = std::malloc(size);
        if (!allocated_memory) {
            throw std::bad_alloc();
        }
    
        alloc.track(allocated_memory, container);
        return allocated_memory;
    }
    
    void operator delete(void* p, const Allocator& alloc, const void* container)
    {
        alloc.release(p, container);
        std::free(p);
    }
    
    int main()
    {
        Allocator alloc;
        int* i = new (alloc, NULL) int;
        operator delete(i, alloc, NULL);
    }
    

    Overloading operator new and using a function deallocate:

    #include <new>
    #include <cstdlib>
    
    struct Allocator {
        void track(void* p, const void* container) const;
        void release(void* p, const void* container) const;
    };
    
    void* operator new (size_t size, const Allocator& alloc, const void* container)
    {
        void* allocated_memory = std::malloc(size);
        if (!allocated_memory) {
            throw std::bad_alloc();
        }
    
        alloc.track(allocated_memory, container);
        return allocated_memory;
    }
    
    template<typename T> void deallocate(T* p, const Allocator& alloc, const void* container)
    {
        p->~T();
        alloc.release(p, container);
        std::free(p);
    }
    
    int main()
    {
        Allocator alloc;
        int* i = new (alloc, NULL) int;
        deallocate(i, alloc, NULL);
    }
    

    One case that you should consider is what to do when new succeeds but constructing the object fails. That is, when there’s plenty of memory, but some other reason that the object can’t be created. The system is actually smart enough to call the right delete (well, the delete that has a signature that the system thinks it should have) to release the allocated memory; but only if there is a delete with that signature. You may well want to overload operator delete and provide a deallocate function, with one of them being defined in terms of the other:

    void operator delete(void* p, const Allocator& alloc, const void* container)
    {
        alloc.release(p, container);
        std::free(p);
    }
    
    template<typename T> void deallocate(T* p, const Allocator& alloc, const void* container)
    {
        p->~T();
        operator delete(p, alloc, container);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to define a new type and have not had much luck finding
I have a macro where I pass in an argument and use that define
I have a define: hashdefine kPingServerToSeeIfInternetIsOn http://10.0.0.8 then in code I with to use
Suppose I have the following macro: #define xxx(x) printf(%s\n,x); Now in certain files I
I have a macro defined in C something like this: #define SOME_FIELD(_A_,_B_,_C_) \ MyObj[
i have a macro that i use to speed up the implementation of Factory
In my code, I have: #define EV( event ) SendEvent( new event ); EV(
I have defined a class A and derived a new class B from A
I have a script which creates a user-defined object like this: obj = new
I have a Java array defined already e.g. float[] values = new float[3]; I

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.