Why memory isn't zero out from malloc?
I have read in the book:
A dynamic memory allocator maintains an area of a process's virtual memory known as the heap. Details vary from system to system, but without loss of generality, we will assume that the heap is an area of demand-zero memory that begins immediately after the uninitialized bss area and grows upward (toward higher addresses).
So, I am confused why memory from heap isn't initialized with zero. To be more precise I mean a chunk of memory returned by malloc.
What this book is describing is an example of how memory allocation can work. It's a very common example, but there are platforms that work differently. It describes a multitasking platform with virtual memory.
There are two aspects to memory allocation on a multitasking platform: first the task receives some memory from the operating system; then the task itself manages its own memory. The paragraph you cited describes the first step.
On this typical platform, when the task asks the operating system for more memory (by making a system call to the kernel, eg with the brk
system call on traditional Unix systems), the kernel of the operating system finds some physical memory that isn't used by any other task, marks it in its internal data structures as being in use, and references them in the task's memory map at the end of a consecutive segment of virtual addresses called BSS or heap. Furthermore this memory is zeroed out so that the task won't read some data left over by another task.
The malloc
function works by allocating chunks of the heap area. It works entirely inside the task's heap. It only makes a system call to obtain more memory if the heap is full. On most platforms, the malloc
function does not overwrite the memory that it allocates, for performance. So when a block of memory is used for the first time after it's been obtained from the operating system, it'll happen to be zeroed out. But if the memory has already been used inside the task because it's been obtained with malloc
, then freed with free
, and reused by another malloc
, then the memory will contain whatever the task put there the first time around.
To cut the long story short, because malloc()
is not designed that way.
When allocating memory from heap, it just returns a pointer to the memory with the requested size, it does not bother about the (existing) content of the memory location. If the memory was previously being allocated by some other call and then released, then there is a possibility that the actual memory still holding the previous data. The same memory location, if gets re-allocated by a next call to malloc()
, it may very well contain the old data (which becomes indeterminate in the last call). It actually saves the overhead of zeroing-out the allocated memory.
When you're immediately going to overwrite the allocated memory, a malloc()
can save you an unnecessary overhead of zeroing out the memory location.
OTOH, calloc()
does the zero-initialization of the allocated memory, if you need that.
上一篇: 使用堆栈或堆之间的区别
下一篇: 为什么内存不会从malloc中清零?