aboutsummaryrefslogtreecommitdiffstats
path: root/src/pdclib/functions/stdlib/calloc.c
blob: d736065911e96aa1bced8dada0191092953737d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* void * calloc( size_t, size_t )

   This file is part of the Public Domain C Library (PDCLib).
   Permission is granted to use, modify, and / or redistribute at will.
*/

#include <stdlib.h>
#include <string.h>

#ifndef REGTEST

void * calloc( size_t nmemb, size_t size )
{
    /* assign memory for nmemb elements of given size */
    void * rc = malloc( nmemb * size );
    if ( rc != NULL )
    {
        /* zero-initialize the memory */
        memset( rc, 0, nmemb * size );
    }
    return rc;
}

#endif

#ifdef TEST

#include "_PDCLIB_test.h"

int main( void )
{
    char * s;
    TESTCASE( ( s = calloc( 3, 2 ) ) != NULL );
    TESTCASE( s[0] == '\0' );
    TESTCASE( s[5] == '\0' );
    free( s );
    TESTCASE( ( s = calloc( 6, 1 ) ) != NULL );
    TESTCASE( s[0] == '\0' );
    TESTCASE( s[5] == '\0' );
    free( s );
    TESTCASE( ( s = calloc( 1, 6 ) ) != NULL );
    TESTCASE( s[0] == '\0' );
    TESTCASE( s[5] == '\0' );
    free( s );
    return TEST_RESULTS;
}

#endif