4 ms·
Shouldn't? malloc doesn't make any guarantees that the memory is initialised or not. In the Linux malloc man page it specifically says it is not cleared, on Mac
by calloc 16y ago
Shouldn't? malloc doesn't make any guarantees that the memory is initialised or not. In the Linux malloc man page it specifically says it is not cleared, on Mac OS X it doesn't specifically mention that it is initialised. On FreeBSD it says it is initialised
Yes, programmers should make sure that memory is indeed cleared, preferably by using calloc or memset.
I just ran a test on my FreeBSD laptop, allocating 256 MB of ram (on a 64 MB physical ram laptop), and then walking through it, and with just malloc I got all 0's. I then had the program write random out to those 256 MB, free it, re-run the program, still no garbage values. Same on Mac OS X. I don't currently have access to a Linux machine so it is not possible for me to run the test there.
So apparently that way may run on Mac OS X and FreeBSD could fail on Linux.