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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:03:04+00:00 2026-05-27T06:03:04+00:00

If I call: char *myChar = (char *)malloc(sizeof(char)); I am likely to be using

  • 0

If I call:

char *myChar = (char *)malloc(sizeof(char));

I am likely to be using more than 1 byte of memory, because malloc is likely to be using some memory on its own to keep track of free blocks in the heap, and it may effectively cost me some memory by always aligning allocations along certain boundaries.

My question is: Is there a way to find out how much memory is really used up by a particular malloc call, including the effective cost of alignment, and the overhead used by malloc/free?

Just to be clear, I am not asking to find out how much memory a pointer points to after a call to malloc. Rather, I am debugging a program that uses a great deal of memory, and I want to be aware of which parts of the code are allocating how much memory. I’d like to be able to have internal memory accounting that very closely matches the numbers reported by top. Ideally, I’d like to be able to do this programmatically on a per-malloc-call basis, as opposed to getting a summary at a checkpoint.

  • 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-27T06:03:05+00:00Added an answer on May 27, 2026 at 6:03 am

    There isn’t a portable solution to this, however there may be operating-system specific solutions for the environments you’re interested in.

    For example, with glibc on Linux, you can use the mallinfo() function from <malloc.h> which returns a struct mallinfo. The uordblks and hblkhd members of this structure contains the dynamically allocated address space used by the program including book-keeping overhead – if you take the difference of this before and after each malloc() call, you will know the amount of space used by that call. (The overhead is not necessarily constant for every call to malloc()).

    Using your example:

    char *myChar;
    size_t s = sizeof(char);
    struct mallinfo before, after;
    int mused;
    
    before = mallinfo();
    myChar = malloc(s);
    after = mallinfo();
    
    mused = (after.uordblks - before.uordblks) + (after.hblkhd - before.hblkhd);
    
    printf("Requested size %zu, used space %d, overhead %zu\n", s, mused, mused - s);
    

    Really though, the overhead is likely to be pretty minor unless you are making a very very high number of very small allocations, which is a bad idea anyway.

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

Sidebar

Related Questions

I am using the code below char call[64] = {'\0'} /* clean buffer */
void foo(char *p) { int i; int len = strlen(p); p = malloc(sizeof(char)*len+2); p[0]
Every now and then I need to call new[] for built-in types (usually char
I call a javascript function from a textbox by using OnKeyPress=clickSearchButton() Here is my
I call this a flash meeting, but maybe there is another more appropriate name.
Please see the following statements: char a[5]=jgkl; // let's call this Statement A char
I would like to call this C++ function from my C# code: void GetArrayOfNames(char**
During a call to function B() from function A(), B() allocates a 100-char array
Suppose the char of ▣ is in somefont.ttf's glyph table. char = unichr(9635) subprocess.call(['convert',
#include < stdio.h > int main() { char *s; s=call(); printf(s); } char* call()

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.