title: Usage of ptrdiff_t
body: <p>I'm implementing iterator through continuous chunk of memory and came to the issue about its conforming usage. My current implementation (assuming I'm iterating through array of <code>char</code>s).</p>
  
  <pre><code>typedef struct iterator{
      void *next_ptr;
      void *limit; //one past last element pointer
  } iterator_t;
  
  void *next(iterator_t *iterator_ptr){
      void *limit = iterator_ptr -&gt; limit;
      void *next_ptr = iterator_ptr -&gt; next_ptr;
      ptrdiff_t diff = limit - next_ptr;
      if(diff &lt;= 0){
          return NULL;
      }
      iterator_ptr -&gt; next_ptr = ((char *) next_ptr) + 1;
      return next_ptr;
  }
  </code></pre>
  
  <p>The issue is the Standard claims at <code>6.5.6(p9)</code> that:</p>
  
  <blockquote>
    <p>When two pointers are subtracted, both shall point to elements of the
    same array object,or  one  past  the  last  element  of  the  array 
    object</p>
  </blockquote>
  
  <p>This is true. I assume the area I'm iterating through is an array.</p>
  
  <blockquote>
    <p>If  the  result  is  not  representable  in  an  object  of  that 
    type,  the  behavior  is  undefined.  In other words, if the
    expressions point to, respectively, the <code>i</code>-th and <code>j</code>-th elements of
    an array object, the expression <code>(P)-(Q)</code> has the value <code>i−j</code> provided
    the value fits in an object of type <code>ptrdiff_t</code>.</p>
  </blockquote>
  
  <p>The limits of <code>ptrdiff_t</code> are defined at <code>7.20.3(p2)</code>:</p>
  
  <blockquote>
    <p>limits of <code>ptrdiff_t</code></p>
    
    <p><code>PTRDIFF_MIN</code>                       −65535 </p>
    
    <p><code>PTRDIFF_MAX</code>                       +65535</p>
  </blockquote>
  
  <p>There is no any guarantees that all values represented with <code>size_t</code> should be represented with <code>ptrdiff_t</code>.</p>
  
  <p>So we judging by the limits we can <strong><em>conformingly</em></strong> subtract pointers of an array only of <code>65535</code> elements at most? So this would not work in general case where I want to subtract two pointers to elements of an array of unknown size?</p>