title: Coalescing the memory and dealing with pointer arithmetic in C
body: <p>In the following code, I don't know why I never enter the if. It makes sense to me that it goes inside the if:</p>
  
  <pre><code>void Mem_Coalesce(){
  
  
      list_t* temp;
      temp = freep;
          if(free_node_count == 2){
          //will finish
               printf("Addr of temp is  %llx\n\n", (long long unsigned) temp);
                   printf("Size of Temp is  %d\n\n", temp-&gt;size);
               printf("Addr of end of temp free node space is  %llx\n\n", (long long unsigned) ((char*)temp + temp-&gt;size));
               printf("Addr of start temp-&gt;next  is  %llx\n\n", (long long unsigned) temp-&gt;next);
          if( ((char*)temp + (temp-&gt;size)) == (char*)(temp-&gt;next)){
                  printf("Entered the if statement\n");
                          temp-&gt;size += (temp-&gt;next)-&gt;size;
                  temp-&gt;next = NULL;
          }
  
  
      }else {
  
          while(temp-&gt;next != NULL  ){
  
              if( ((char*)temp + (temp-&gt;size)) == (char*)(temp-&gt;next)){
  
                  temp-&gt;size += (temp-&gt;next)-&gt;size;
                                  printf("coalesced size is %d temp-&gt;size \n", temp-&gt;size);
                  temp-&gt;next = (temp-&gt;next)-&gt;next;
                  ((temp-&gt;next)-&gt;next)-&gt;prev = temp;
              }
              temp=temp-&gt;next;
  
          }
  
      }
  
  
  
  }
  </code></pre>
  
  <p>The results is </p>
  
  <pre><code>Addr of temp is  7f9c1e89b070
  
  Size of Temp is  200
  
  Addr of end of temp free node space is  7f9c1e89b138
  
  Addr of start temp-&gt;next  is  7f9c1e89b278
  </code></pre>
  
  <p>And as you see it never enters <code>if statement</code>. Also let me know if there's any other algorithm for memory coalescing which might be more efficient.</p>