_debug_realloc - Reallocate Memory Block

Format

#include <stdlib.h>  /* also in <malloc.h> */
void *_debug_realloc(void *ptr, size_t size,
                     const char *file, size_t line);

Language Level: Extension

_debug_realloc is the debug version of realloc. Like realloc, it reallocates the block of memory pointed to by ptr to a new size, specified in bytes. It also sets any new memory it allocates to 0xAA, so you can easily locate instances where your program tries to use the data in that memory without initializing it first.

In addition, _debug_realloc makes an implicit call to _heap_check, and stores the file name file and the line number line where the storage is reallocated. This information can be used later by the _heap_check, _dump_allocated, or _dump_allocated_delta functions.

If ptr is NULL, _debug_realloc behaves like _debug_malloc (or malloc) and allocates the block of memory.

To use _debug_realloc, you must compile with the debug memory -qheapdebug compiler option. This option maps all realloc calls to _debug_realloc.

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.

Because _debug_realloc always checks what heap the memory was allocated from, you can use _debug_realloc to reallocate memory blocks allocated by the regular or debug versions of the memory management functions. However, if the memory was not allocated by the memory management functions, or was previously freed, _debug_realloc generates an error message and the program ends.

Return Value
_debug_realloc
returns a pointer to the reallocated memory block. The ptr argument to _debug_realloc is not the same as the return value; _debug_realloc always changes the memory location to help you locate references to the memory that were not freed before the memory was reallocated.

If size is 0, _debug_realloc returns NULL. If not enough memory is available to expand the block to the given size, the original block is unchanged and NULL is returned.

Example
This example uses _debug_realloc to allocate 100 bytes of storage. It then attempts to write to storage that was not allocated. When _debug_realloc 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 realloc calls to _debug_realloc.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
   char *ptr;
   if (NULL == (ptr = (char*)realloc(NULL, 100))) {
      puts("Could not allocate memory block.");
      exit(EXIT_FAILURE);
   }
   memset(ptr, 'a', 105);     /* overwrites storage that was not allocated    */
   ptr = (char*)realloc(ptr, 200);        /*  realloc invokes _heap_check     */
   puts("_debug_realloc did not detect that a memory block was overwritten." );
   return 0;
   /****************************************************************************
      The output should be similar to:
      End of allocated object 0x00073890 was overwritten at 0x000738f4.
      The first eight bytes of the memory block (in hex) are: 6161616161616161.
      This memory block was (re)allocated at line number 8 in _debug_reall.c.
      Heap state was valid at line 8 of _debug_reall.c.
      Memory error detected at line 13 of _debug_reall.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_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_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