title: Memory Allocation in C using mmap and memset
body: <p>I need some help understanding why I'm getting a segmentation fault after I run my code, or why it doesn't stop when the memory used exceeds memory. I've been trying to do various things to see where the cp and ip are and most of the time they're the same. I've built this code off of what our professor gave us. Different version of the code that I wrote will give me "p6 should have been NULL, but is <em>address</em>". 
  I'm just really trying to understand this, I'm not looking for anyone to post code for me. 
  Any help or suggestions are appreciated. I've searched Google, on here, youtube, but havent been able to find any help. Yes, I realize this sucks so please dont be rude Im just trying to learn. Thanks.</p>
  
  <pre><code>      1
        2 #include &lt;stdio.h&gt;
        3 #include &lt;string.h&gt;
        4 #include &lt;errno.h&gt;
        5 #include &lt;sys/mman.h&gt;
        6 #include &lt;sys/types.h&gt;
        7 #include &lt;unistd.h&gt;
        8
        9     int mem,memused,i, pgsz, *ip;
       10     unsigned char *cp;
       11     void *region;
       12
       13 void heap_init(int num_pages_for_heap)
       14 {
       15     pgsz = getpagesize();
       16     pgsz*=num_pages_for_heap;
       17      mem=pgsz;
       18     region = mmap(NULL, pgsz, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
       19     if (region == ((void *) -1))
       20     {
       21         perror("mmap");
       22        // return 1;
       23     }
       24
       25 }
       26
       27 void *heap_alloc(int num_bytes_to_allocate)
       28 {
       29  if(num_bytes_to_allocate%16 != 0)
       30   {  num_bytes_to_allocate+=8;}
       31
       32     memused+=num_bytes_to_allocate;
       33 if(mem&lt;memused)
       34 { cp=NULL;
       35   return cp;
       36 }
       37
       38 else{
       39  printf("mem used: %d mem: %d region: %p  %d\n",memused,mem,region,&amp;region);
       40    cp = (unsigned char *)region;
       41    *(cp+num_bytes_to_allocate)='a';
       42
       43     ip = (int *)region;
       44    //printf("ip %p, %d\n",ip, &amp;ip);
       45
       46   // *(ip+num_bytes_to_allocate);  // region+1004
       47   // printf("ip %p, %d\n",ip, &amp;ip);
       48   //  *ip+=(num_bytes_to_allocate);
       49
       50     //printf("ip %p\n",ip);
       51     // cp = (unsigned char *)region;
       52     //*cp+=(num_bytes_to_allocate);
       53     //printf("cp %p, %d\n",cp,&amp;cp);
       54
       55  for (i=999; i &lt; num_bytes_to_allocate; i++)//I dont understand why the prof used 999.
       56     {
       57       *(cp+i);
       58
       59     }
       60     printf("\n");
       61
       62
       63 return cp;
       64 }
       65 }
       66 void heap_free(void *pointer_to_area_to_free)
       67 {
       68  //   return;
       69 }
  </code></pre>
  
  <p>//Output for current code used: </p>
  
  <pre><code>  mem used: 2000 mem: 8192 region: 0x7f8a62941000  6296176
  
    mem used: 4000 mem: 8192 region: 0x7f8a62941000  6296176
  
    mem used: 6000 mem: 8192 region: 0x7f8a62941000  6296176
  
    mem used: 7008 mem: 8192 region: 0x7f8a62941000  6296176
  
    mem used: 8016 mem: 8192 region: 0x7f8a62941000  6296176
  
    Segmentation fault
  </code></pre>
  
  <p>// This is the driver program he gave us:</p>
  
  <pre><code>  1 #include &lt;stdio.h&gt;
    2 #include &lt;stdlib.h&gt;
    3 #include &lt;string.h&gt;
    4
    5 void heap_init(int num_pages_for_heap);
    6 void *heap_alloc(int num_bytes_to_allocate);
    7 void heap_free(void *pointer_to_area_to_free);  // not used in this test
    8
    9 int main(int argc, char *argv[])
   10 {
   11     char *p1, *p2, *p3, *p4, *p5, *p6;
   12
   13     heap_init(2);
   14
   15     p1 = (char *) heap_alloc(2000);
   16     if ((long int)p1 % 16 != 0)
   17     {
   18         printf("p1 bad %p  pmod16 %d\n",p1,((long int)p1)%16);
   19         exit(-1);
   20     }
   21     memset(p1,'X',2000);
   22
   23     p2 = (char *) heap_alloc(2000);
   24     if ((long int)p2 % 16 != 0)
   25     {
   26         printf("p2 bad %p  pmod16 %d\n",p2,((long int)p2)%16);
   27         exit(-1);
   28     }
   29     memset(p2,'X',2000);
   30
   31     p3 = (char *) heap_alloc(2000);
   32     if ((long int)p3 % 16 != 0)
   33     {
   34         printf("p3 bad %p  pmod16 %d\n",p3,((long int)p3)%16);
   35         exit(-1);
   36     }
   37     memset(p3,'X',2000);
   38
   39     p4 = (char *) heap_alloc(1000);
   40     if ((long int)p4 % 16 != 0)
   41     {
   42         printf("p4 bad %p  pmod16 %d\n",p4,((long int)p4)%16);
   43         exit(-1);
   44     }
   45     memset(p4,'X',1000);
   46
   47     p5 = (char *) heap_alloc(1000);
   48     if ((long int)p5 % 16 != 0)
   49     {
   50         printf("p5 bad %p  pmod16 %d\n",p5,((long int)p5)%16);
   51         exit(-1);
   52     }
   53     memset(p5,'X',1000);
   54
   55
   56     p6 = (char *) heap_alloc(1500);  // try 1500 first
   57     if (p6 != NULL)
   58     {
   59         printf("p6 should have been NULL, but is %p\n",p6);
   60         exit(-1);
   61     }
   62
   63     p6 = (char *) heap_alloc(50);   // then just get 50
   64     if ((long int)p6 % 16 != 0)
   65     {
   66         printf("p6 bad %p  pmod16 %d\n",p6,((long int)p6)%16);
   67         exit(-1);
   68     }
   69     memset(p6,'X',50);
   70
   71     printf("DONE\n");
   72
   73     return 0;
   74 }
  </code></pre>