How To Get The Size Of Memory Allocated In Heap

sizeof() function can not give the size of memory locations allocated using new, malloc, calloc and realloc. VC++ provides a macro for the above purpose and it is _msize. See the following code segment.

#include <iostream>
using namespace std;
int main()
{  
   // Allocate 10 integer locations 
   // = a toatl of 40 in 32 bit platforms
   int* pInt = new int[10];
   size_t Size = _msize(pInt);
   cout << Size << '\n';   
   return 0;
}

The result will be 40.

Leave a Reply