Everything

calloc [V1.02 or later]


Allocates memory that has been initialized by zero.

[Classification]

Standard library

[Syntax]

#include <stdlib.h>

void __near * __far calloc(size_t nmemb, size_t size);

[Return value]

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

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

[Description]

This function allocates an area whose size is specified by size and the number of elements in an array is specified by nmemb and then initializes that area by 0.

 

[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);

-

Do not define the __heap_chk_fail function as static.

-

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 allocate 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.