realloc [V1.02 or later]


Re-allocates memory.

[Classification]

Standard library

[Syntax]

#include <stdlib.h>

void __near * __far realloc(void __near *ptr, size_t size);

[Return value]

Upon succeeding to allocate an area, the pointer to that area is returned.

If size is 0 or the area could not be allocated, a null pointer is returned.

[Description]

This function changes the area indicated by ptr to the size specified by size.

The size before re-allocation and the contents of the area up to the smaller value of size do not change.

If the size is increased, the area for the increased part is not initialized.

If ptr is a null pointer, operation is the same as that for "malloc (size)".

If ptr is not a null pointer and size is 0, operation is the same as that for "free (ptr)".

In addition to that, if ptr is not an area allocated by calloc, malloc, or realloc, or ptr has already been released by free or realloc, operation is not guaranteed.

 

[Professional Edition only] [V1.03 or later]

When using a malloc library for the security facility, the __heap_chk_fail function is called when one of the following operations is performed.

-

The pointer to an area other than that allocated by calloc, malloc, or realloc is passed to free or realloc.

-

The pointer to an area released by free is passed again to free or realloc.

-

After calloc, malloc, or realloc, a value is written to an address outside the allocated area (within two bytes before and after the allocated area) and the pointer to that area is passed to free or realloc.

 

The __heap_chk_fail function needs to be defined by the user and it describes the processing to be executed when an error occurs in management of dynamic memory.

Note the following points when defining the __heap_chk_fail function.

-

The __heap_chk_fail function should be a far function whose return value and parameter type should be the void type.
void __far __heap_chk_fail(void);

-

Corruption of heap memory area should not be detected recursively in the __heap_chk_fail function.

 

The calloc, malloc, and realloc functions for the security facility secure four extra bytes before and after each allocated area for the purpose of detecting writing to addresses outside the allocated area. This consumes more heap memory area than with the usual functions.

[Caution]

The default size of the heap memory area is 0x100 bytes.

To change the heap memory area, define the _REL_sysheap array and set the array size in the _REL_sizeof_sysheap variable.

[Example of setting the heap memory area]
#include <stddef.h>
#define SIZEOF_HEAP  0x200
char _REL_sysheap[SIZEOF_HEAP];
size_t _REL_sizeof_sysheap = SIZEOF_HEAP;

Remark

The _REL_sysheap array should be allocated to an even address.