The calloc() Function
Besides the malloc() function, you can also use the calloc() function to allocate a memory storage dynamically. The differences between the two functions are that the latter takes two arguments and that the memory space allocated by calloc() is always initialized to 0. There is no such guarantee that the memory space allocated by malloc() is initialized to 0.
The syntax for the calloc() function is#include <stdlib.h>
void *calloc(size_t nitem, size_t size);
Here nitem is the number of items you want to save in the allocated memory space. size gives the number of bytes that each item takes. The calloc() function returns a void pointer too.
If the calloc() function fails to allocate a piece of memory space, it returns a null pointer.
Listing 17.3 contains an example of using the calloc() function. The initial value of the memory space allocated by calloc() is printed out.
TYPE
Listing 17.3. Using the calloc() function.1: /* 17L03.c: Using the calloc() function */
2: #include <stdio.h>
3: #include <stdlib.h>
4: /* main() function */
5: main()
6: {
7: float *ptr1, *ptr2;
8: int i, n;
9: int termination = 1;
10:
11: n = 5;
12: ptr1 = calloc(n, sizeof(float));
13: ptr2 = malloc(n * sizeof(float));
14: if (ptr1 == NULL)
15: printf("malloc() failed.\n");
16: else if (ptr2 == NULL)
17: printf("calloc() failed.\n");
18: else {
19: for (i=0; i<n; i++)
20: printf("ptr1[%d]=%5.2f, ptr2[%d]=%5.2f\n",
21: i, *(ptr1 + i), i, *(ptr2 + i));
22: free(ptr1);
23: free(ptr2);
24: termination = 0;
25: }
26: return termination;
27: }
The following output appears on the screen after running the executable 17L03.exe:
OUTPUT
C:\app>17L03
ptr1[0] = 0.00, ptr2[0] = 7042.23
ptr1[1] = 0.00, ptr2[1] = 1427.00
ptr1[2] = 0.00, ptr2[2] = 2787.14
ptr1[3] = 0.00, ptr2[3] = 0.00
ptr1[4] = 0.00, ptr2[4] = 5834.73
C:\app>
ANALYSIS
The purpose of the program in Listing 17.3 is to use the calloc() function to allocate a piece of memory space. To prove that the calloc() function initializes the allocated memory space to 0, the initial values of the memory are printed out. Also, an-
other piece of memory space is allocated by using the malloc() function, and the initial values of the second memory space is printed out too.
As you see in line 12, the calloc() function is called with two arguments passed to it: the int variable n and the sizeof(float) expression. The float pointer variable ptr1 is assigned the value returned by the calloc() function.
Likewise, the malloc() function is called in line 13. This function only takes one argument that specifies the total number of bytes that the allocated memory should have. The value returned by the malloc() function is then assigned to another float pointer variable, ptr2.
From lines 12 and 13, you can tell that the calloc() and malloc() functions actually plan to allocate two pieces of memory space with the same size.
The if-else-if-else statement in lines 14_25 checks the two values returned from the calloc() and malloc() functions and then prints out the initial values from the two allocated memory spaces if the two return values are not null.
I ran the executable program in Listing 17.3 several times. Each time, the initial value from the memory space allocated by the calloc() function was always 0. But there is no guarantee for the memory space allocated by the malloc() function. The output shown here is one of the results from running the executable program on my machine. You can see that there is some "garbage" in the memory space allocated by the malloc() function. That is, the initial value in the memory is unpredictable. (Sometimes, the initial value in a memory block allocated by the malloc() function is 0. But it is not guaranteed that the initial value is always 0 each time when the malloc() function is called.)
- 40 views