title: Where does malloc() allocate memory? Is it the data section or the heap section of the virtual address space of the process?
body: <p>Ever since I was introduced to <code>C</code>, I was told that in <code>C</code> dynamic memory allocation is done using the functions in the <code>malloc</code> family. I also learned that memory dynamically allocated using <code>malloc</code> is allocated on the heap section of the process.</p>
  <p><a href="https://i.stack.imgur.com/DxXLJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DxXLJ.png" alt="enter image description here" /></a></p>
  <p>Various OS textbooks say that <code>malloc</code> involves system call (though not always but at times) to allocate structures on heap to the process. Now supposing that <code>malloc</code> returns pointer to chunk of bytes allocated on the heap, why should it need a system call. The activation records of a function are placed in the stack section of the process and since the &quot;stack section&quot; is already a part of the virtual address space of the process, pushing and popping of activation records, manipulation of stack pointers, just start from the highest possible address of the virtual address space. It does not even require a system call.</p>
  <p>Now on the same grounds since the &quot;heap section&quot; is also a part of the virtual address space of the process, why should a system call be necessary for allocating a chunk of bytes in this section. The routine like <code>malloc</code> could self handle the &quot;free&quot; list and &quot;allocated&quot; list on its own. All it needs to know is the end of the &quot;data section&quot;. Certain texts say that system calls are necessary to &quot;attach memory to the process for dynamic memory allocation&quot;, but if <code>malloc</code> allocates memory on &quot;heap section&quot; why is it at all required to attach memory to the process during <code>malloc</code>? Could be simply taken from portion already part of the process.</p>
  <p>While going through the text &quot;The C Programming Language&quot; [2e] by Kernighan and Ritchie, I came across their implementation of the <code>malloc</code> function [section 8.7 pages 185-189]. The authors say :</p>
  <blockquote>
  <p><code>malloc</code> calls upon the operating system to obtain more memory as necessary.</p>
  </blockquote>
  <p>Which is what the OS texts say, but counter intuitive to my thought above (if <code>malloc</code> allocates space on heap).</p>
  <p>Since asking the system for memory is a comparatively expensive operation, the authors do not do that on every call to <code>malloc</code>, so they create a function <code>morecore</code> which requests at least <code>NALLOC</code> units; this larger block is chopped up as needed. And the basic free list management is done by <code>free</code>.</p>
  <p>But the thing is that the authors use <code>sbrk()</code> to ask the operating system for memory in <code>morecore</code>. Now Wikipedia says:</p>
  <blockquote>
  <p><code>brk</code> and <code>sbrk</code> are basic memory management system calls used in Unix and Unix-like operating systems to control the amount of memory allocated to the <em><strong>data segment</strong></em> of the process.</p>
  </blockquote>
  <p>Where</p>
  <blockquote>
  <p>a data segment (often denoted .data) is a portion of an object file or the corresponding address space of a program that contains <strong>initialized static variables, that is, global variables and static local variables.</strong></p>
  </blockquote>
  <p>Which I guess is not the &quot;heap section&quot;. [Data section is the second section from bottom in the picture above, while heap is the third section from bottom.]</p>
  <hr />
  <p>I am totally confused. I want to know what really happens and how both the concepts are correct? Please help me understand the concept by joining the scattered pieces together...</p>