Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 13 Jun 2005 20:58:38 -0600 (MDT)
From:      "M. Warner Losh" <imp@bsdimp.com>
To:        mhunter@ack.berkeley.edu
Cc:        des@des.no, freebsd-hackers@freebsd.org
Subject:   Re: unitialized memory is all zeros...why not garbage instead?
Message-ID:  <20050613.205838.56966326.imp@bsdimp.com>
In-Reply-To: <20050613193150.GA75218@malcolm.berkeley.edu>
References:  <20050610224058.GA11336@malcolm.berkeley.edu> <86vf4lb110.fsf@xps.des.no> <20050613193150.GA75218@malcolm.berkeley.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
In message: <20050613193150.GA75218@malcolm.berkeley.edu>
            Mike Hunter <mhunter@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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050613.205838.56966326.imp>