_msize -- Return Number of Bytes Allocated

Format

#include <stdlib.h>  /* also in <malloc.h> */
size_t _msize(void *ptr)

Language Level: Extension
_msize determines the number of bytes that were allocated to the pointer argument ptr. The ptr must have been returned from one of the runtime memory allocation functions (_ucalloc, malloc, and so on).

You cannot pass the argument of an object that has been freed.

Return Value
_msize returns the number of bytes allocated. If the argument is not a valid pointer returned from a memory allocation function, the return value is undefined. If NULL is passed, _msize returns 0.

Example
This example displays the size of an allocated object from malloc.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
   char *ptr;
   if (NULL == (ptr = (char*)malloc(10))) {
      puts("Could not allocate memory block.");
      exit(EXIT_FAILURE);
   }
   memset(ptr, 'x', 5);
   printf("The size of the allocated object is %u.\n",_msize(ptr));
   return 0;
   /****************************************************************
      The output should be similar to:
      The size of the allocated object is 10.
   ****************************************************************/
}



calloc -- Reserve and Initialize Storage
malloc -- Reserve Storage Block
realloc -- Change Reserved Storage Block Size
<malloc.h>
<stdlib.h>