Microsoft KB Archive/59087

{|
 * width="100%"|

Determining Size of Memory Block Referenced by a Pointer

 * }

Q59087

-

The information in this article applies to:


 * Microsoft C for OS/2, versions 5.1, 6.0, and 6.0a
 * Microsoft C/C++ for MS-DOS, versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, 7.0
 * Microsoft Visual C++, versions 1.0, 1.5, 2.0

-

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.

Sample Code
/* Compile options needed: none 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.
 * 1) include 
 * 2) include 
 * 1) include 

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  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(&quot;Memory allocation error\n&quot;); return; }  size = msize(p,&error); if(size == 0) printf(&quot;An msize error has occurred\n&quot;); else printf(&quot;Size = %u\n&quot;, 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.
 * 1) include 

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

Keywords :

Issue type :

Technology : kbVCsearch kbAudDeveloper kbPTProdChange kbvc150 kbvc100 kbZNotKeyword3 kbCVC700DOS kbVC200