Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes Malloc only use the heap if requested memory space is large?
    primarykey
    data
    text
    <p>Whenever you study the memory allocation of processes you usually see it outlined like this:</p> <p><img src="https://i.stack.imgur.com/wn0oN.jpg" alt="enter image description here"></p> <p>So far so good.</p> <p>But then you have the sbrk() system call which allows the program to change the upper limit of its <strong>data section</strong>, and it can also be used to simply check where that limit is with sbrk(0). Using that function I found the following patterns:</p> <p><strong>Pattern 1 - Small malloc</strong></p> <p>I run the following program on my Linux machine:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; int globalVar; int main(){ int localVar; int *ptr; printf("localVar address (i.e., stack) = %p\n",&amp;localVar); printf("globalVar address (i.e., data section) = %p\n",&amp;globalVar); printf("Limit of data section = %p\n",sbrk(0)); ptr = malloc(sizeof(int)*1000); printf("ptr address (should be on stack)= %p\n",&amp;ptr); printf("ptr points to: %p\n",ptr); printf("Limit of data section after malloc= %p\n",sbrk(0)); return 0; } </code></pre> <p>And the output is the following:</p> <pre><code>localVar address (i.e., stack) = 0xbfe34058 globalVar address (i.e., data section) = 0x804a024 Limit of data section = 0x91d9000 ptr address (should be on stack)= 0xbfe3405c ptr points to: 0x91d9008 Limit of data section after malloc= 0x91fa000 </code></pre> <p>As you can see the allocated memory region was right above the old data section limit, and after the malloc that limit was pushed upward, so the allocated region is actually inside the new data section. </p> <p><strong>Question 1</strong>: Does this mean that small mallocs will allocate memory in the data section and not use the heap at all? </p> <p><strong>Pattern 2 - Big Malloc</strong></p> <p>If you increase the requested memory size on line 15:</p> <pre><code>ptr = malloc(sizeof(int)*100000); </code></pre> <p>you will now the following output:</p> <pre><code>localVar address (i.e., stack) = 0xbf93ba68 globalVar address (i.e., data section) = 0x804a024 Limit of data section = 0x8b16000 ptr address (should be on stack)= 0xbf93ba6c ptr points to: 0xb750b008 Limit of data section after malloc= 0x8b16000 </code></pre> <p>As you can see here the limit of the data section has not changed, and instead the allocated memory region is in the middle of the gap section, between the data section and the stack.</p> <p><strong>Question 2</strong>: Is this the large malloc actually using the heap?</p> <p><strong>Question 3</strong>: Any explanation for this behavior? I find it a bit insecure, cause on the first example (small malloc) even after you free the allocated memory you'll still be able to use the pointer and use that memory without getting a seg fault, as it will be inside your data section, and this could lead to hard to detect bugs.</p> <p><strong>Update with Specs</strong>: Ubuntu 12.04, 32-bits, gcc version 4.6.3, Linux kernel 3.2.0-54-generic-pae.</p> <p>Update 2: Rodrigo's answer below solved this mystery. <a href="http://en.wikipedia.org/wiki/Data_segment" rel="noreferrer">This Wikipedia link</a> also helped.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload