title: Memory access optimization in C
body: <p>I have to write fast right-trimming function in C. My first attempt to do it was:  </p>
  
  <pre><code>void TrimRight( char *s )
  {
      char* pS;
      if (!*s)
          return;
      if (!s)
          return;
      for ( pS = s + strlen(s) - 1; isspace(*pS) &amp;&amp; (pS &gt;= s); pS--)
      {  
          pS[1] = '\0';
      }
  }
  </code></pre>
  
  <p>Second attempt:</p>
  
  <pre><code>void TrimRight( char *s )
  {   
      if (!*s)
          return;
      if (!s)
          return;
      char* pS = s + strlen(s) - 1;
      while ((*pS == ' ') &amp;&amp; (pS &gt; s))
      {
          (*pS--) = '\0';
      }
  }
  </code></pre>
  
  <p>The question is: Did I improve memory accesses here? How can I optimize this even more? How to optimize memory access in general?    </p>
  
  <p>Upd: Is it true that scanning memory in increasing sequential order is faster?</p>