|
|
|
|
|
by mtlmtlmtlmtl
1115 days ago
|
|
The calloc part is one of the most common blind spots I see among C programmers. I try to avoid the malloc(n * sizeof (...)) pattern as much as possible. Sure there are lots of cases where it can never overflow, and you might save a bit of overhead from the zeroing and overflow checking, but most of that overhead might also be imaginary depending on allocator internals, and even kernel internals. It's the sort of thing it only makes sense to optimise when you've already squeezed out every bit of performance. And by then you've probably minimised dynamic allocation as much as possible anyway. It's also very easy to think something like "well, n is passed in as a parameter, but it's a static function, and I know all the callers. So it's fine". But now every caller in the future has to be aware of this possibility. |
|
Can you clarify: what possibility should you be aware off with malloc that you don't need to be aware of with calloc?