unitialized memory is all zeros...why not garbage instead?
M. Warner Losh
imp at bsdimp.com
Tue Jun 14 02:59:54 GMT 2005
In message: <20050613193150.GA75218 at malcolm.berkeley.edu>
Mike Hunter <mhunter at ack.berkeley.edu> writes:
: Is the pre-zeroing of malloc'd memory documented somewhere? By my reading
: of the malloc manapge...
:
: The calloc() function allocates space for number objects, each size
: bytes in length. The result is identical to calling malloc() with an
: argument of ``number * size'', with the exception that the allocated
: memory is explicitly initialized to zero bytes.
:
: ...it seems like it's saying that malloc (as opposed to calloc) is NOT
: pre-zeroed. Is there a different document I should be reading?
The memory isn't given to the process by malloc. It is given to it by
some other means. That memory is zeroed for security reasons. The
first time malloc returns the memory, with our current implementation,
it will be all zeros. After that, all bets are off with out
implementation. One should not rely on this behavior because one
never knows when the first malloc happens, nor if our malloc might
start writing into the memory it is about to return...
Warner
More information about the freebsd-hackers
mailing list