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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:01:36+00:00 2026-05-14T03:01:36+00:00

I am currently writing a small custom memory Allocator in C++, and want to

  • 0

I am currently writing a small custom memory Allocator in C++, and want to use it together with operator overloading of new/ delete. Anyways, my memory Allocator basically checks if the requested memory is over a certain threshold, and if so uses malloc to allocate the requested memory chunk. Otherwise the memory will be provided by some fixedPool allocators. that generally works, but for my deallocation function looks like this:

void MemoryManager::deallocate(void * _ptr, size_t _size){
    if(_size > heapThreshold)
        deallocHeap(_ptr);
    else 
        deallocFixedPool(_ptr, _size);
}

So I need to provide the size of the chunk pointed to, to deallocate from the right place.

Now the problem is that the delete keyword does not provide any hint on the size of the deleted chunk, so I would need something like this:

void operator delete(void * _ptr, size_t _size){ 
    MemoryManager::deallocate(_ptr, _size); 
}

But as far as I can see, there is no way to determine the size inside the delete operator.- If I want to keep things the way it is right now, would I have to save the size of the memory chunks myself?

  • 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-14T03:01:36+00:00Added an answer on May 14, 2026 at 3:01 am

    allocate more memory than neccessary and store the size information there. That’s what your system allocator probably does already. Something like this (demonstrate with malloc for simplicity):

    void *allocate(size_t size) {
        size_t *p = malloc(size + sizeof(size_t));
        p[0] = size;           // store the size in the first few bytes
        return (void*)(&p[1]); // return the memory just after the size we stored
    }
    
    void deallocate(void *ptr) {
        size_t *p = (size_t*)ptr; // make the pointer the right type
        size_t size = p[-1];      // get the data we stored at the beginning of this block
    
        // do what you need with size here...
    
        void *p2 = (void*)(&p[-1]); // get a pointer to the memory we originally really allocated
        free(p2);                   // free it
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really new to C -> Python interaction and am currently writing a small
I'm currently writing some pipes-core/attoparsec plumbing for a small project of mine. I want
I'm currently writing a small script for use on one of our servers using
I'm writing a small Lisp interpreter in C#, and it's basically working already. Currently
I am currently considering writing a small game. It is essentially a map where
I'm writing a small free tool. It's currently in Beta testing using .NET 3.5
I'm currently writing a program in c# wpf with a custom window. So because
I'm currently writing a small script using pygame, but I don't believe this question
I am currently writing a small application with Python (3.1), and like a good
I am currently writing a small graphical performance test benchmark for JavaFX. Thus, 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.