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

  • Home
  • SEARCH
  • 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 8560549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:18:03+00:00 2026-06-11T16:18:03+00:00

Possible Duplicate: How to get memory block length after malloc? If I have a

  • 0

Possible Duplicate:
How to get memory block length after malloc?

If I have a pointer, is it possible to learn how many bytes were allocated by new?
When I googled I found a solution for Windows: _msize() and for Mac: malloc_size(). But nothing for Linux.

And if not, does anybody know why is it hidden from a programmer? delete should definitely know such info.

Update:

As far as I know, if I have this code:

 class A {   
   ~A() {}
   int m_a; 
 }; 
 class B : public A {
   ~B() {}
   int m_b; 
 };

 int main() {   A * b = new B();   delete b;   return 0; }

Destructor of A will be called, but still all the memory allocated by new will be freed.
This means that it can be somehow computed knowing only the pointer. So what is the reason for hiding it from a programmer?

  • 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-11T16:18:04+00:00Added an answer on June 11, 2026 at 4:18 pm

    Unfortunately, there is no portable way of obtaining the number of bytes allocated by new and malloc. There are a number of reason why this is the case:

    • On some platforms, delete and free do nothing at all. As such, they don’t need to store size information. This is surprisingly common in embedded platforms; it lets you use C or C++ code written for other platforms unchanged, as long as you don’t do too much allocation.
    • Even on more common platforms, the system may allocate a different number of bytes than you ask for. Typically your allocation will be aligned to some larger size – possibly much larger than your original request. The storage metadata might also be stored in a very slow data structure – you wouldn’t want to be taking locks and accessing a hash table in time-critical code.

    As portable languages, C and C++ can’t offer a feature that won’t be available (or well-defined, or reasonably fast) on every platform. That’s why this is not available on C++. That said, you don’t need this – C++ offers std::vector, which does track the size of your allocation, or std::string which takes care of all of those details for you.

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

Sidebar

Related Questions

Possible Duplicate: Does this type of memory get allocated on the heap or the
Possible Duplicate: What REALLY happens when you don't free after malloc? Is freeing allocated
Possible Duplicate: Does this type of memory get allocated on the heap or the
Possible Duplicate: How to get the length of a function in bytes? I'm making
Possible Duplicate: Is it possible to get the pointer the continous memory fragment in
Possible Duplicate: Return value from thread I want to get the free memory of
Possible Duplicate: How to profile and and get Javascript performance I have a page
Possible Duplicate: Pointer to local variable Can a local variable's memory be accessed outside
Possible Duplicate: Error : get property of non-object I am new to codeigniter model,
Possible Duplicate: Get the length of an audio file php i am using php

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.