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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
}

Perform the compilation.

1
gcc run_your_application.c -o run_your_application -I/usr/local/ksl/include -L/usr/local/ksl/lib

Run your application.

1
LD_PRELOAD=/usr/local/ksl/lib/libkqmalloc.so ./run_your_application

Output:

1
calloc pointer address: ffffafe4c690