calloc() (正確には public_cALLOc()) 実装の glibc malloc.c では、2 つの方法で実行しているメモリを 0 にしようとすると、バイト数が 36 より大きい場合、すぐに memset() が呼び出されますそれ以外の場合は、具体的には次のようにバイトごとに 0 を設定します。
glibc-2.13/malloc/malloc.c
void * public_cALLOc()
{
.....
int_malloc();
...
...
/* Unroll clear of <= 36 bytes (72 if 8byte sizes). We know that
contents have an odd number of INTERNAL_SIZE_T-sized words;
minimally 3. */
...
if (nclears > 9)
MALLOC_ZERO(d, clearsize); /* this is nothing but memset(d,0,clearsize) */
else {
*(d+0) = 0;
*(d+1) = 0;
if (nclears > 4) {
*(d+2) = 0;
*(d+3) = 0;
if (nclears > 6) {
*(d+4) = 0;
*(d+5) = 0;
if (nclears > 8) {
*(d+6) = 0;
*(d+7) = 0;
*(d+8) = 0;
}
}
}
---------------------------------
問題は、なぜ memset() を直接実行しないのか、この区別の必要性は何かということです。
ありがとう、カピル