I have just concluded a happy 4.5 hours of debugging a nasty leak in my system.
It turns out I was doing this:
params = allocate(sizeof(Something*) * num_params);
which in turns essentially calls malloc
with the first argument passed in. When num_params
is 0, it would call malloc(0)
.
Running this in a loop, the program would very quickly take up most of the memory. I fixed it by first checking if num_params == 0
, and if so avoiding the call to allocate
.
I know that the Standard dictates malloc(0)
is implementation-defined. So, how is this implemented in the Windows 7 C runtime library, and why does it cause a leak?
Edit: Clarifying the question - why does malloc(0)
on Windows allocate memory, and what is the logic that dictates how much memory will be allocated?