title: What does size of the memcmp return value mean?
body: <p>I just happened to debug an incredibly nasty error: On my own PC (Windows 7 x64, MinGw) my C program would successfully sort an array using the <code>memcmp</code> when comparing array members.</p>
  
  <p>My function used bubble sort algorithm and it's skeleton would look like this:</p>
  
  <pre><code>void array_sort(ArrayMap *array, char direction) {
      make sure direction is +1 or -1 only
      define some variables
      for(...) {
           for(...) {
               cmpres = memcmp(elm1, elm2, sizeOfElement);
               if (cmpres!=0&amp;&amp;cmpres!=direction)
               {
                   SWAP here
               }
           }
  }
  </code></pre>
  
  <p>Now while on my PC, the <code>memcmp</code> has returned <code>-1</code>, <code>0</code> and <code>1</code> on another it returned <code>-5</code>, <code>0</code> and <code>5</code>. By comparing this with <code>direction</code> I caused the sorting to go totally wrong.</p>
  
  <p>But I wonder, what does the absolute value (that is, the size) of the return value of <code>memcmp</code> actually mean?</p>
  
  <p>From the <a href="http://www.cplusplus.com/reference/cstring/memcmp/" rel="nofollow">www.cplusplus.com</a>:</p>
  
  <blockquote>
    <p>Returns an integral value indicating the relationship between the
    content of the memory blocks: A zero value indicates that the contents
    of both memory blocks are equal. A value <strong>greater than zero</strong> indicates
    that the first byte that does not match in both memory blocks has a
    greater value in ptr1 than in ptr2 as if evaluated as unsigned char
    values; And a value <strong>less than zero</strong> indicates the opposite.</p>
  </blockquote>
  
  <p>No mention of the size, they just make sure not to be wrong about +-1 by saying <em>greater than zero</em>.</p>