_debug_heapmin -- Release Unused Memory in the Default Heap

Format

#include <stdlib.h>  /* also in <malloc.h> */
int _debug_heapmin(const char *file, size_t line);

Language Level: Extension
_debug_heapmin is the debug version of _heapmin. Like _heapmin, it returns all unused memory from the default runtime heap to the operating system.

In addition, _debug_heapmin makes an implicit call to _heap_check, and stores the file name file and the line number line where the memory is returned. This information can be used later by the _heap_check function.

To use _debug_heapmin, you must compile with the debug memory (/Tm) compiler option. This option maps all _heapmin calls to _debug_heapmin.

Note: The /Tm option maps all calls to memory management functions (including a heap-specific version and a tiled version for OS/2) to their debug counterparts. To prevent a call from being mapped, parenthesize the function name.

A heap-specific and tiled for OS/2 versions of this function (_debug_uheapmin and _debug_theapmin) are also available. _debug_heapmin always operates on the default heap.

Return Value
If successful, _debug_heapmin returns 0; otherwise, it returns -1.

Example
This example allocates 10000 bytes of storage, changes the storage size to 10 bytes, and then uses _debug_heapmin to return the unused memory to the operating system. The program then attempts to overwrite memory that was not allocated. When _debug_heapmin is called again, _heap_check detects the error, generates several messages, and stops the program.

Note: You must compile this example with the /Tm option to map the _heapmin calls to _debug_heapmin.

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
   char *ptr;
   /* Allocate a large object from the system */
   if (NULL == (ptr = (char*)malloc(100000))) {
      puts("Could not allocate memory block.");
      exit(EXIT_FAILURE);
   }
   ptr = (char*)realloc(ptr, 10);
   _heapmin();                /* No allocation problems to detect             */
   *(ptr - 1) = 'a';          /* Overwrite memory that was not allocated      */
   _heapmin();                /* This call to _heapmin invokes _heap_check    */
   puts("_debug_heapmin did not detect that a non-allocated memory block"
        "was overwritten.");
   return 0;
   /****************************************************************************
      Possible output is:
      Header information of object 0x000738b0 was overwritten at 0x000738ac.
      The first eight bytes of the memory block (in hex) are: AAAAAAAAAAAAAAAA.
      This memory block was (re)allocated at line number 13 in _debug_heapm.c.
      Heap state was valid at line 14 of _debug_heapm.c.
      Memory error detected at line 17 of _debug_heapm.c.
   ****************************************************************************/
}


Managing Memory with Multiple Heaps
Memory Management


_dump_allocated -- Get Information about Allocated Memory
_dump_allocated_delta -- Get Information about Allocated Memory
_heapmin -- Release Unused Memory from Default Heap
_heap_check -- Validate Default Memory Heap
<malloc.h>
<stdlib.h>
/Tm compiler option