IBM C and C++ Compilers provides two sets of functions for debugging your memory problems:
Debug Memory Management Functions
Debug versions of the heap-specific memory management
functions are provided, just as they are for the regular
versions. Each debug version performs the same function as its
non-debug counterpart. In addition, the debug version calls _uheap_check
to check the heap used in the call, and records the file and line
number where the memory was allocated or freed. You can then use _dump_allocated
or _dump_allocated_delta to display
information about currently allocated memory blocks. Information
is printed to stderr.
You can use debug memory management functions for any type of heap, including shared memory. To use the debug versions, specify the -qheapdebug compiler option. IBM C and C++ Compilers then maps all calls to memory management functions (regular or heap-specific) to the corresponding debug versions.
Note: If you parenthesize the name of a memory management function, the function is not mapped to the debug version.
Heap-Checking Functions
IBM C and C++ Compilers also provides some new functions
for validating user heaps: _uheapchk, _uheapset,
and _uheap_walk. Each of these functions
also has a non-heap-specific version that validates the default
heap.
Both _uheapchk and _uheapset check the specified heap for minimal consistency; _uheapchk checks the entire heap, while _uheapset checks only the free memory. _uheapset also sets the free memory in the heap to a value you specify. _uheap_walk traverses the heap and provides information about each allocated or freed object to a callback function that you provide. You can then use the information however you like.
These heap-checking functions are defined in <umalloc.h> (the regular versions are also in <malloc.h>). They are not controlled by a compiler option, so you can use them in your program at any time.
Which Should I Use?
Both sets of debugging functions have their benefits and
drawbacks. Which you choose to use depends on your program, your
problems, and your preference.
The debug memory management functions provide detailed information about all allocation requests you make with them in your program. You don't need to change any code to use the debug versions; you need only specify the -qheapdebug compiler option. However, because only calls that have been mapped to debug versions provide any information, you may have to rebuild many or all of your program's modules, which can be time-consuming.
On the other hand, the heap-checking functions perform more general checks on the heap at specific points in your program. You have greater control over where the checks the occur. The heap-checking functions also provide compatibility with other compilers that offer these functions. You only have to rebuild the modules that contain the heap-checking calls. However, you have to change your source code to include these calls, which you will probably want to remove in your final code. Also, the heap-checking functions only tell you if the heap is consistent or not; they do not provide the details that the debug memory management functions do.
What you may choose to do is add calls to heap-checking functions in places you suspect possible memory problems. If the heap turns out to be corrupted, at that point you may want to rebuild with the -qheapdebug option.
Note: When the debug memory option -qheapdebug is specified, code is generated to pre-initialize the local variables for all functions. This makes it much more likely that uninitialized local variables will be found during the normal debug cycle rather than much later (usually when the code is optimized).
Regardless of which debugging functions you choose, your program requires additional memory to maintain internal information for these functions. If you are using fixed-size heaps, you may have to increase the heap size in order to use the debugging functions.
Memory Management Functions
Managing Memory with Multiple Memory Heaps
Types of Memory
Creating and Using a Fixed Size
Heap
Creating and Using an Expandable
Heap
Debugging Problems with Heap
Memory
Changing the Default Heap Used in
a Program
Example of Creating and Using a
User Heap
Example of Creating and Using a
Shared-Memory User Heap
-qheapdebug Compiler
Option
_debug_calloc - Allocate and
Initialize Memory
_debug_free - Free Allocated Memory
_debug_heapmin - Free Unused Memory
in the Default Heap
_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