title: How to test the WKdm algorithm against buffers of various sizes in C
body: <p>I'm trying to test the WKdm algorithm to see how well it performs against buffers of 100KB, 1MB, and 10MB; however, any buffer greater than 1KB in my test program below throws EXC_BAD_ACCESS:Could not access memory.  </p>
  
  <p>I took WKdm.c's main(), which is a simple test, and tried to convert it so that i could change the size of the input buffer to be compressed.  </p>
  
  <p>I'm using the standard Scott Kaplan implementation of the WKDM algorithm which consists of a source and header file, found <a href="http://www.google.com/url?q=http://www.cs.amherst.edu/~sfkaplan/research/compressed-caching/WKdm.tgz&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNEgwxY6HJxWUO21I8l2Uz6b7lrI4Q" rel="nofollow">here</a>.  I have tried on Linux and OS X 32-bit.  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;math.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;sys/time.h&gt;
  #include "WKdm.h"
  
  #define PAGE_SIZE_IN_WORDS 1024
  #define PAGE_SIZE_IN_BYTES 4096
  
  int main() {
  
      WK_word i;
      //int testSize = 1024; //1KB Works
      int testSize = 102400; //100KB causes EXC_BAD_ACCESS
  
      int testNumWords = testSize / sizeof(WK_word);
      printf("testSize = %d bytes or %d words\n", testSize, testNumWords);
  
      WK_word* source_buf = (WK_word*) malloc(testSize * 2);
      WK_word* dest_buf = (WK_word*) malloc(testSize * 2);
      WK_word* udest_buf = (WK_word*) malloc(testSize * 2);
  
      for (i = 0; i &lt; testNumWords; i++) {
          source_buf[i] = rand() % 1000; //Semi-random: 0-999 stored in each 4-byte word
      }
  
      source_buf[testNumWords + 1] = 99999;
      udest_buf[testNumWords + 1] = 55555;
  
      printf("first 50 words of source_buf are:\n");
      for (i = 0; i &lt; 50; i++)
          printf(" %d", source_buf[i]);
      fflush(stdout);
  
      struct timeval t0;  struct timeval t1;
      gettimeofday(&amp;t0, 0);
  
      // Compress the source_buf into the dest_buf
      i = WKdm_compress(source_buf, dest_buf, testNumWords);
  
      gettimeofday(&amp;t1, 0);
      long elapsed = (t1.tv_sec - t0.tv_sec) * 1000000 + t1.tv_usec - t0.tv_usec;
  
      printf("\nWKdm_compress size in bytes: %u\n", i);
      printf("Time to compress: %lu microseconds\n\n", elapsed);
  
      printf("redzone value at end of source buf (should be 99999) is %u\n",
              source_buf[testNumWords + 1]); fflush(stdout);
  
      gettimeofday(&amp;t0, 0);
  
      WKdm_decompress(dest_buf, udest_buf, testNumWords);
  
      gettimeofday(&amp;t1, 0);
      elapsed = (t1.tv_sec - t0.tv_sec) * 1000000 + t1.tv_usec - t0.tv_usec;
      printf("Time to decompress: %lu microseconds\n\n", elapsed);
  
      printf("redzone value at end of udest buf (should be 55555) is %u\n", udest_buf[testSize + 1]);
  
      printf("first 50 words of udest_buf are:\n");
      for (i = 0; i &lt; 50; i++)
          printf(" %d", udest_buf[i]);
  
      i = bcmp(source_buf, udest_buf, 100);
  
      printf("\nbcmp of orig. and compr'd/decompr'd copy (should be 0) is %u\n", i);
      fflush(stdout);
      return 0;
  }
  </code></pre>