2 ms·
If you turn on wall you should be warned that you didn’t include the right headers? I was under the impression that lots of implementations of malloc actually
by danieltillett 11y ago
If you turn on wall you should be warned that you didn’t include the right headers?
I was under the impression that lots of implementations of malloc actually call calloc under the hood. Unless you are calling malloc in a tight loop I can’t see the extra step of zeroing being a issue. In one of my programs where performance is critical I changed over all my mallocs to calloc and couldn’t measure the time change - I did find I had fewer difficult-to-duplicate bugs though.
Edit. Interestingly CERT recommends casting [1]. Seems to solve a much worse issue than forgetting to include stdlib.h
1. https://www.securecoding.cert.org/confluence/display/c/MEM02-C.+Immediately+cast+the+result+of+a+memory+allocation+function+call+into+a+pointer+to+the+allocated+type https://www.securecoding.cert.org/confluence/display/c/MEM02...