Determining Size of Memory Block Referenced by a Pointer

ID: Q59087


The information in this article applies to:


SUMMARY

In Microsoft C, there is no C run-time library function that explicitly returns the amount of memory that has been allocated to a pointer by malloc(). However, it is possible to retrieve this information indirectly through the Microsoft C run-time library function _heapwalk(). The _heapwalk() function returns a heap information structure of the following form:


   struct _heapinfo {
       int far * _pentry;
       size_t _size;
       int _useflag;
   } _HEAPINFO; 
The information returned in this structure can be accessed directly or used in writing a function that gives the size information you want.


MORE INFORMATION

Sample Code


/* Compile options needed: none
*/ 
#include <stddef.h>
#include <malloc.h>
size_t msize(void * pointer, int * error)
{
   struct _heapinfo info; /* Heap info structure */ 
   int   heap_status;     /* Heap status */ 
   size_t  ret_val;       /* msize() return value */ 
   info._pentry = NULL;   /* Initialize heap info structure */ 
   do {
      heap_status = _heapwalk(&info); /* Step through heap until */ 
   } while ((pointer != info._pentry) /* desired entry is found  */ 
     && (heap_status == _HEAPOK));   /* or a heap error occurs. */ 
   if(heap_status == _HEAPOK)
      if(info._useflag != _FREEENTRY)
         ret_val = info._size;          /* No errors */ 
      else {
         ret_val = 0;                  /* Entry has been freed */ 
         *error = _FREEENTRY;
      }
   else {
      ret_val = 0;     /* A heap error occurred */ 
      *error = heap_status;
   }
   return(ret_val);
} 
NOTE: The msize() function takes as a parameter a pointer pointing to the base memory that was allocated. For msize() to work correctly, this pointer MUST point to the base of the allocated block, and not anywhere within that block.

The msize() function returns the size of the allocated block or -1 if an error occurs. If -1 is returned, an error code is returned through the error parameter. The error parameter will be one of the following manifest constants:

   _FREENTRY      _HEAPEMPTY      _HEAPBADPTR
   _HEAPBADBEGIN  _HEAPBADNODE    _HEAPEND 
These error codes are generated by the Microsoft run-time library function _heapwalk(). Refer to your Microsoft C Compiler documentation on _heapwalk() for more information on the meaning of these error codes.

Following is a sample program that uses the msize() function to find the amount of memory allocated to a pointer. This function is designed to work in any memory model:

Sample Program


#include <stdio.h>
#include <malloc.h>
size_t msize(void *, int *);
void main (void)
{
   char * p;
   int    error;
   size_t size;
   p = (char *)malloc(sizeof(char) * 11);
   if(p == NULL) {
      printf("Memory allocation error\n");
      return;
   }
   size = msize(p,&error);
   if(size == 0)
      printf("An msize error has occurred\n");
   else
      printf("Size = %u\n", size);
} 
NOTE: The malloc() function allocates 11 bytes of memory for use by the pointer p. Surprisingly, msize() returns 12 as the size of the allocated block. Actually, this is an expected result. Malloc() always allocates even-byte amounts as a result of PC architecture. Consequently, msize()'s return value is always an even amount.

Additional query words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50 2.00


Keywords          : kb16bitonly 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: July 22, 1999