1

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.

Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
  • 1
    That's a different kind of "heap". In fact, the C standard has no notion of "heap". The word means many different things depending on the context. – Kerrek SB Sep 27 '15 at 17:34
  • 9
    Because it's not required to. There's `calloc` for that. – Theodoros Chatzigiannakis Sep 27 '15 at 17:34
  • 1
    Why would it be? There's no advantage, and the performance hit could be significant. If you want dynamic memory to be initialized to zero, then use `calloc(3)`. – Filipe Gonçalves Sep 27 '15 at 17:34
  • 3
    What's "the book"? Is it an authoritative description of how C works? Judging by the use of phrases like "without loss of generality", it seems to be more focused on teaching how systems *can* work, not how specific systems *do*. – user2357112 Sep 27 '15 at 17:37
  • ok. I will ask in another way: What does mean highlight part of my first post? – Gilgamesz Sep 27 '15 at 17:42
  • There is an awesome link to clarify your doubts [here](http://www.geeksforgeeks.org/memory-layout-of-c-program/) – Rahul Jha Sep 27 '15 at 17:47
  • 1
    `malloc` doesn't necessarily request new memory from the heap. It can also return memory that was `free`d by your program. – melpomene Sep 27 '15 at 17:51
  • I've never heard the term **demand-zero memory**. I suspect it is bullshit. – wildplasser Sep 27 '15 at 17:54

2 Answers2

4

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, e.g. 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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
0

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261