PRODUCT : Borland C++ NUMBER : 1711 VERSION : All OS : All DATE : October 25, 1993 PAGE : 1/3 TITLE : Functions to find the size of memory block allocated An integral segment of programming is the use of variables that store important data. Due to the nature of declaring and defining variables, an aspect to consider is the amount of memory available to the programmer with which to store data in these variables. When a programmer allocates memory for storage he/she is sometimes inclined to forget the size of allocated memory for that variable. This is especially true for pointer data types. And, if the total allocation of memory exceeds the total pool of resources available, numerous surprises will occur in the programmer's code. This document will cover two functions that will help any programmer "remember" the size of allocated memory for any pointer type in any memory model. The functions that will reveal to us the size of memory in question is called _msize() for NEAR memory models and for FAR memory models _fmsize(). (Note SAMPLE1 below) The mechanism that these functions use is very simple. When memory is allocated there is a data-structure "associated" to the block of memory. This struct is defined in ALLOC.H. (Note SAMPLE2 below) The interesting portion of this structure is the second data- member; "unsigned int size" for the NEAR models and "unsigned long" size for the FAR models. This data member contains the size of the block of memory associated to the pointer in question. What _msize() and _fmsize() accomplish is to walk the heap and locate the address of the desired pointer and then "remember" the size of the block which is located in the struct associated with the pointer and return the size of the block. Before this heap walk occurs, assert() is used to verify that the heap is valid and that no corruption has occurred by calling heapcheck() and farheapcheck(). Another fascinating item that must not be overlooked is the nature of struct heapinfo and struct farheapinfo. The location of the heap in a NEAR memory model is all the space from the end of the data segment and the top of the program stack. In a FAR memory model the heap is located from the end of the program stack to the end of available memory. In a FAR model one can allocate memory until conventional memory (640k) is exhausted. But, using the run-time function malloc() a programmer is limited to 64k of memory allocation resources in a NEAR model. By using the #if defs in SAMPLE1 the programmer can have their code declare and call the appropriate function, based on the memory model being utilized. Although when using far pointers in a NEAR data model the programmer must be responsible and call _fmsize() PRODUCT : Borland C++ NUMBER : 1711 VERSION : All OS : All DATE : October 25, 1993 PAGE : 2/3 TITLE : Functions to find the size of memory block allocated in order to obtain correct results. To properly use the _msize() and _fmsize() functions the programmer must insert all of SAMPLE1 into their code. The #if defs will determine which function to call based on the memory model the program is built with. If a programmer has code built in the small memory model, and uses a far character pointer, then _fmsize() must be called in order to find the correct size of the block of memory associated with that pointer. Once again, make note of the type of pointers being used, and make the appropriate call. SAMPLE1 #include #if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__) unsigned int _msize(void *p) { unsigned int size = 0; struct heapinfo hi; /*check heap*/ assert (heapcheck() != _HEAPCORRUPT); /*walk heap*/ hi.ptr = NULL; while(heapwalk(&hi) == _HEAPOK) { if(hi.ptr == p) size = hi.size; } return (size); } #else unsigned long _msize(void *p) { return (_fmsize(p)); } #endif unsigned long _fmsize(void far *p) { unsigned long size = 0; struct farheapinfo hi; /*check heap*/ assert (farheapcheck() != _HEAPCORRUPT); /*walk heap*/ PRODUCT : Borland C++ NUMBER : 1711 VERSION : All OS : All DATE : October 25, 1993 PAGE : 3/3 TITLE : Functions to find the size of memory block allocated hi.ptr = NULL; while( farheapwalk(&hi) == _HEAPOK ) { if(hi.ptr == p) size = hi.size; } return (size); } SAMPLE2 struct heapinfo { void _FAR *ptr; unsigned int size; int in_use; }; struct farheapinfo { void huge *ptr; unsigned long size; int in_use; }; DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.