calloc


Memory allocation (initialized to zero)

[Classification]

Standard library

[Syntax]

#include <stdlib.h>

void *calloc(size_t nmemb, size_t size);

[Return value]

When area allocation succeeds, a pointer to that area is returned. When the area could not be allocated, a null pointer is returned.

[Description]

This function allocates an area for an array of nmemb elements. The allocated area is initialized to zeros.

[Caution]

The memory area management functions automatically allocate memory area as necessary from the heap memory area.

Also, the size of the default is 0x1000 bytes, so when it's changed, the heap memory area must be allocated. The area allocation should be performed first by an application.

 

[Heap memory setup example]

#include    <stddef.h>
#define     SIZEOF_HEAP 0x1000
int     _REL_sysheap[SIZEOF_HEAP >> 2];
size_t  _REL_sizeof_sysheap = SIZEOF_HEAP;

Remark 1.

The variable "_REL_sysheap" points to the starting address of heap memory. This value must be a multiple of 4.

Remark 2.

The required heap memory size (bytes) should be set for the variable "_REL_sizeof_sysheap".

[Example]

#include    <stdlib.h>
typedef struct {
    double  d[3];
    int     i[2];
} s_data;
int func(void) {
    sdata   *buf;
    if((buf = calloc(40, sizeof(s_data))) == NULL)  /*allocate an area for 40 s_data*/
        return(1);
      :
    free(buf);                                      /*release the area*/
    return(0);
}