Determine size of dynamically allocated memory in C

s_itbhu picture s_itbhu · Aug 15, 2009 · Viewed 79k times · Source

Is there a way in C to find out the size of dynamically allocated memory?

For example, after

char* p = malloc (100);

Is there a way to find out the size of memory associated with p?

Answer

ars picture ars · Aug 15, 2009

There is no standard way to find this information. However, some implementations provide functions like msize to do this. For example:

Keep in mind though, that malloc will allocate a minimum of the size requested, so you should check if msize variant for your implementation actually returns the size of the object or the memory actually allocated on the heap.