On 8/16/07, Xavier <shiningxc@gmail.com> wrote:
On Thu, Aug 16, 2007 at 01:36:31PM -0400, Dan McGee wrote:
2. Watch your mallocs, and use calloc when possible. You didn't allocate space for the null byte, so you were overrunning your buffers when you filled them and the free() failed when using mtrace(). I switched to calloc usage, and now use sprintf because this is a case where we can do that- it is faster and we aren't worried about running out of room. We then need to take care of the null byte ourselves, however.
Why is it better to use calloc? And when using calloc, is it still needed to set the null byte a second time?
Calloc initializes any memory to zero, which can help solve debugging issues a lot easier when something goes wrong. In addition, it is very clear how much and of what size memory you need with it. Regarding the null byte thing- calloc zeros the memory, and while I would assume that is the same as the null byte, I don't want to take it for granted, so I figured I'd set it explicitly anyway. -Dan