Question 7.31

What's the difference between calloc and malloc? Is it safe to take advantage of calloc's zero-filling? Does free work on memory allocated with calloc, or do you need a cfree?


calloc(m, n) is essentially equivalent to

p = malloc(m * n);
memset(p, 0, m * n);
The zero fill is all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values. free is properly used to free the memory allocated by calloc.

References: ANSI Sec. 4.10.3 to 4.10.3.2
ISO Sec. 7.10.3 to 7.10.3.2
H&S Sec. 16.1 p. 386, Sec. 16.2 p. 386
PCS Sec. 11 pp. 141,142


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback