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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:57:01+00:00 2026-05-19T09:57:01+00:00

EDITED: reworded question. When new and malloc are called, the size of the block

  • 0

EDITED: reworded question.

When new and malloc are called, the size of the block of memory to be allocated is passed:

void* malloc(size_t);
void* operator new(size_t);

is it possible to get type information, i.e. so you could do a sizeof(T) where T was the type the memory was being allocated for:

T t = new T;

I’d like to overload new and malloc but require the type information not just the size of the memory being allocated.

FURTHER EDIT:

The reason I am doing this is that I will overload malloc. This function will inspect the size of the memory being allocated and allocate from a particualr memory pool:

        template <int v> struct int2type { value = v };

        inline void* malloc(const std::size_t sz)
        {
            if(sz <= 64)
            {
                size_obj* ptr = malloc(nggt::core::int2type<sizeof(size_obj) + (sz % 8)>);
                ptr->sz = sz;
                return ptr+1;
            }
            else if(sz <= 128)
            {
                size_obj* ptr = malloc(nggt::core::int2type<sizeof(size_obj) + 128>);
                ptr->sz = sz;
                return ptr+1;
            }
            else
            {
                return TSuper::malloc(sz);
            }
        }


        inline void* malloc(const nggt::core::int2type<sizeof(size_obj) + 8> sz)
        {
            return m_heap8.malloc(sz.value);
        }

        inline void* malloc(const nggt::core::int2type<sizeof(size_obj) + 16> sz)
        {
            return m_heap16.malloc(sz.value);
        }

There are also supporintg free overloads using a freelist to return memory to a pool. Problem is I can’t use size_t sz in a template as it’s not known at compile time. If I could get the type information I could do sizeof(T) and be done!

Cheers,
Graeme

  • 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-19T09:57:02+00:00Added an answer on May 19, 2026 at 9:57 am

    The new with the std::size_t argument is commonly known as non-placement new. The std::size_t argument is passed in by default to let the operating system know how many bytes to allocate for the object you are creating. You, yourself, don’t need to provide it to the new declaration.

    Edit:

    In response to your edit. It’s common practice if you are overloading the new operator to

    1. place it within the namespace you would like it to be called from
    2. Avoid a namespace new if you can make specific news for your objects. (that is, there is behavior specific to each class, why not place it with the class itself)
    3. The first argument should be the std::size_t which you don’t need to worry about. It’ll be handled for you.

    Everything else after that is all yours:

    struct MyObject{
        MyObject* operator new(std::size_t s, const char* message){
            cout << "Creating an object of size " << size_of(MyObject) << endl;
            return new MyObject();
        }
    };
    

    You can even use the “old” new in your call of your “new” new.

    Further Edit:

    In response to your next edit, don’t call your own malloc, malloc. Instead, call it some other name. If you’re writing C++, malloc has no place within your code what so ever unless you’re dealing with some legacy application. In C++, you should only be touching the new operator.

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

Sidebar

Related Questions

Edited 3.9.2011 to add one more related question Hello, I'm very new to WCF
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
edited, this is it function handleDate( timestamp ) { var n=new Date(), t, ago
EDITED * * Hello I am new to PHP and trying to figure out
EDITED QUESTION Hi this is the total workflow of this project we have a
Edited to make the question more clear. I am trying to work with Isolates
Edited this question because of my bad examples.. Here is my updated question: Would
Edited question... Hello, I would like to load a .xhtml file of my composite
edited : My file has several lines. I encrypt the file onto a new
[EDITED: I left the original question below, with some more context and code to

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.