With all due respect, Corona688, you're the one that's confused.
I am aware that allocation and paging in are discreet steps. What I wrote above in no way suggests that memset() does not incur further overhead beyond sbrk or malloc. Quite the contrary, I ran that memset() to demonstrate that even with the further overhead and work, zeroing 2 GB of ram does not take very long.
As for the rest of my post, the gist is that there is no difference between calloc and malloc for a large allocation whose pages aren't already mapped in.
Quote:
Originally Posted by
Corona688
If you're allocating enough memory to approach SIZE_MAX, you really,
really don't want to zero it first
Why not? If the memory needs to be zeroed, it needs to be zeroed. The size of the allocation is irrelevant. For a small allocation using a recycled chunk, the memset hit is negligible. For the huge allocations discussed, if there is sufficient memory for the allocation to succeed, calloc() knows that the allocation will be backed by fresh pages that are already zeroed. Knowing this, calloc() will not call memset(), and so no paging in will occur until the memory is written to. In the end, calloc() and malloc() are equivalent.
Regards,
Alister