Determining Size of Memory Block Referenced by a Pointer

Last reviewed: July 17, 1997
Article ID: Q59087
6.00 6.00a 6.00ax 7.00 | 1.00 1.50 | 5.10 6.00 6.00a | 1.00 2.00
MS-DOS                 | WINDOWS   | OS/2            | WINDOWS NT
kbprg kbfasttip

The information in this article applies to:

  • Microsoft C for MS-DOS, versions 5.0, 5.1, 6.0, 6.0a, and 6.0ax
  • Microsoft C for OS/2, versions 5.1, 6.0, and 6.0a
  • Microsoft C/C++ for MS-DOS, versions 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 1.5
  • Microsoft Visual C++, 32-bit Edition, versions 1.0 and 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.

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 reference words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50 2.00
KBCategory: kbprg kbfasttip
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.