title: Correctness of 'afree' function in K&R
body: <p>In the chapter <em>Address Arithmetic</em> the function <code>afree</code> is defined as follows:</p>
  
  <pre class="lang-c prettyprint-override"><code>static char allocbuf[ALLOCSIZE]; // storage for alloc
  static char *allocp = allocbuf;  // next free position
  
  void afree(char *p) 
  {
      if (p &gt;= allocbuf &amp;&amp; p &lt; allocbuf + ALLOCSIZE)
          allocp = p;
  }
  </code></pre>
  
  <p>As far as I can see, the point of the if statement is to test whether <code>p</code> is within the bounds of the buffer. </p>
  
  <p>However, if <code>p</code> is not within the bounds of the buffer, the comparision results in undefined behavior, doesn't it? Isn't it necessary to convert both pointers (<code>p</code> and <code>allocbuf</code>) to integers before doing the comparision?</p>
  
  <p>Should the function not look like this or is this version even wrong?</p>
  
  <pre class="lang-c prettyprint-override"><code>void afree(char *p) 
  {
      uintptr_t p_int = (uintptr_t) p;
      uintptr_t a_start = (uintptr_t) allocbuf;
      uintptr_t a_end = (uintptr_t) (allocbuf + ALLOCSIZE);
  
      if (p_int &gt;= a_start &amp;&amp; p_int &lt; a_end)
          allocp = p;
  }
  
  </code></pre>