title: Segmentation fault when blurring an images matrix. [C]
body: <p>This is a simplified version of what my program does, my program is intended to create random terrain from a heightmap. This is how I am creating the heightmap.</p>
  
  <p>I have run this through gdb but when I run it through gdb, it never seg faults. When I run it normally however it segfaults. I have no idea why, shouldn't it work the same either way. Is there a tool that I could use to visualize the memory usage of my program?</p>
  
  <p>I am trying to blur a matrix which have access to pixel values. the matrix is indexed row by column. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;limits.h&gt;
  #include &lt;time.h&gt;
  
  #define MAX(X, Y) ((X) &gt; (Y) ? (X) : (Y))
  #define MIN(X, Y) ((X) &lt; (Y) ? (X) : (Y))
  
  typedef struct {
      int width;
      int height;
      double ** mat;
  } grid_t;
  
  static double r2()
  {
      return (double)rand() / (double)RAND_MAX ;
  }
  
  grid_t * create_grid(int width, int height) {
      grid_t * grid = malloc(sizeof(grid));
      grid-&gt;width = width;
      grid-&gt;height = height;
      grid-&gt;mat = malloc(height*sizeof(double *));
      int i = 0;
      for(; i &lt; height; i++){
          grid-&gt;mat[i] = malloc(width*sizeof(double));
      }
      return grid;
  }
  
  grid_t * clone_grid(grid_t * grid) {
      grid_t * clone = create_grid(grid-&gt;width, grid-&gt;height);
      int i = 0;
      for(; i &lt; clone-&gt;height; i++){
          int j = 0;
          for(; j &lt; clone-&gt;width; j++){
              clone-&gt;mat[i][j] = grid-&gt;mat[i][j];
          }
      }
      return clone;
  }
  
  void init_rand(grid_t * grid) {
      int i = 0;
      for(; i &lt; grid-&gt;height; i++) {
          int j = 0;
          for(; j &lt; grid-&gt;width; j++) {
              //srand(time(NULL) + rand()%time(NULL)); uncomment this for the weird effect.
              grid-&gt;mat[i][j] = r2();
          }
      }
  }
  
  
  void free_grid(grid_t * grid) {
      int i = 0;
      for(; i &lt; grid-&gt;height; i++) {
          free(grid-&gt;mat[i]);
      }
      free(grid-&gt;mat);
  }
  
  void blur_grid(grid_t * grid, int radius) {
      printf("blurring...");
      grid_t * clone = clone_grid(grid);
      int i, j, x, y;
      for(i = 0; i &lt; grid-&gt;height; i++) {
          int x_min = MAX(0, i-radius), x_max = MIN(clone-&gt;height, i+radius);
          for(j = 0; j &lt; grid-&gt;width; j++) {
              int y_min = MAX(0, j-radius), y_max = MIN(clone-&gt;width, j+radius);
              unsigned int sum = 0;
              int count = 0;
              for(x = x_min; x &lt; x_max; x++){
                  for(y = y_min; y &lt; y_max; y++){
                      sum += clone-&gt;mat[x][y];
                      count++;
                  }
              }
              grid-&gt;mat[i][j] = sum / count;
          }
      }
      printf("blurred\n");
      free_grid(clone);
  }
  
  int main() {
      int width = 512;
      int height = 1024;
      int blur_iter = 1;
      int blur_radius = 2;
  
      grid_t * grid = create_grid(width, height);
  
      init_rand(grid);
  
      int i = 0;
      for(; i &lt; blur_iter; i++) {
          blur_grid(grid, blur_radius);
      }
  
      free_grid(grid);
  
      return 0;
  }
  </code></pre>
  
  <p>The result should be a matrix with its values tending towards the global average.</p>
  
  <p>The result I get however is a matrix that is often all zeroes.</p>