4 ms·
First: Lose the cast. It's not needed in C, clutters the code, and can hide real problems (like implicit declarations of the functions -- this can cause crashes
by orib 18y ago
First: Lose the cast. It's not needed in C, clutters the code, and can hide real problems (like implicit declarations of the functions -- this can cause crashes on 64-bit architectures). A void* exists solely so it can be assigned to any type* without casts. It's useless for any other purpose.
Yes, they are effectively the same. I'd suspect that on most systems (glibc, certainly) calloc more or less does:
mem = malloc(nelem * size);
memset(mem, 0, nelem * size);
return mem;
- sysop073 18y agoHe didn't actually say he was using C, and I always include the cast. It's more portable, and doesn't really clutter the code since everyone is used to seeing that exact expression