Rate This Document
Findability
Accuracy
Completeness
Readability

calloc

Function Usage

Allocates contiguous memory blocks and initializes the memory blocks to 0.

Function Syntax

void *calloc(size_t number_of_objects, size_t size_of_object);

Parameters

Parameter

Description

Value Range

Input/Output

number_of_objects

Number of requested memory blocks.

The value cannot be negative.

Input

size_of_object

Number of bytes of the requested memory blocks.

The value cannot be negative.

Input

Return Value

  • Success: A pointer to the allocated memory is returned.
  • Failure: A null pointer is returned.

Example

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "kqmalloc.h"

#define KQMALLOC_TEST_LEN 10

void CallocExample()
{
    int8_t *res = (int8_t *)calloc(KQMALLOC_TEST_LEN, KQMALLOC_TEST_LEN);
    if (res == NULL) {
        printf("res is null\n");
    } else {
        printf("calloc pointer address: %lx\n", res);
        free(res);
    }
}

int main(void) {
    CallocExample();
    return 0;
}

Output:

calloc pointer address: ffffafe4c690