]> pd.if.org Git - pdclib/blob - functions/stdlib/calloc.c
Added comments.
[pdclib] / functions / stdlib / calloc.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* void * calloc( size_t, size_t )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <stdlib.h>
12 #include <string.h>
13
14 #ifndef REGTEST
15
16 void * calloc( size_t nmemb, size_t size )
17 {
18     /* assign memory for nmemb elements of given size */
19     void * rc = malloc( nmemb * size );
20     if ( rc != NULL )
21     {
22         /* zero-initialize the memory */
23         memset( rc, 0, nmemb * size );
24     }
25     return rc;
26 }
27
28 #endif
29
30 #ifdef TEST
31 #include <_PDCLIB_test.h>
32
33 int main()
34 {
35     char * s;
36     BEGIN_TESTS;
37     TESTCASE( ( s = calloc( 3, 2 ) ) != NULL );
38     TESTCASE( s[0] == '\0' );
39     TESTCASE( s[5] == '\0' );
40     free( s );
41     TESTCASE( ( s = calloc( 6, 1 ) ) != NULL );
42     TESTCASE( s[0] == '\0' );
43     TESTCASE( s[5] == '\0' );
44     free( s );
45     TESTCASE( ( s = calloc( 1, 6 ) ) != NULL );
46     TESTCASE( s[0] == '\0' );
47     TESTCASE( s[5] == '\0' );
48     free( s );
49     return TEST_RESULTS;
50 }
51
52 #endif