_debug_heapmin - Free 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 -qheapdebug compiler option. This option maps all _heapmin calls to _debug_heapmin.

Note: The -qheapdebug option maps all calls to memory management functions (including heap-specific versions) to their debug counterparts. To prevent a call from being mapped, parenthesize the function name.

A heap-specific version of this function (_debug_uheapmin) is 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 -qheapdebug 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.
   ****************************************************************************/
}


Debugging Memory Heaps
Memory Management Functions
Managing Memory with Multiple Memory Heaps


Debugging Problems with Heap Memory


-qheapdebug Compiler Option
_debug_calloc - Allocate and Initialize Memory
_debug_free - Free Allocated Memory
_debug_malloc - Allocate Memory
_debug_memcpy - Copy Bytes
_debug_memmove - Copy Bytes
_debug_memset - Set Bytes to Value
_debug_realloc - Reallocate Memory Block
_debug_strcat - Concatenate Strings
_debug_strcpy - Copy Strings
_debug_strncat - Concatenate Strings
_debug_strncpy - Copy Strings
_debug_strnset - Set Characters in String
_debug_strset - Set Characters in String
_debug_ucalloc - Reserve and Initialize Memory from User Heap
_debug_uheapmin - Free Unused Memory in User Heap
_debug_umalloc - Reserve Memory Block from User Heap