title: strange malloc behavior in C
body: <p>I am trying to create a matrix with dynamic proportions and to initialize it
  here is the code I am using to allocate memory and initialization:</p>
  
  <pre><code>int **matrix;
  //mem allocation
  matrix=(int*)malloc(sizeof(int*)*mat_w);
  for (i=0;i&lt;mat_w;i++)
      matrix[i]=(int)malloc(sizeof(int)*mat_h);
  //init
  for (i=0;i&lt;mat_w;i++)
      for (j=0;j&lt;mat_h;j++)
          matrix[i][j]=0;
  </code></pre>
  
  <p>This , works fine , question is , if I try to create a matrix of type short - I get a segmentation error on the init first pass.</p>
  
  <p>Is this a C language issue or am I doing something wrong?</p>
  
  <p>Code for matrix of type <code>short</code>:</p>
  
  <pre><code>short **matrix;
  //mem allocation
  matrix=(short*)malloc(sizeof(short*)*mat_w);
  for (i=0;i&lt;mat_w;i++)
      matrix[i]=(short)malloc(sizeof(short)*mat_h);
  //init
  for (i=0;i&lt;mat_w;i++)
      for (j=0;j&lt;mat_h;j++)
          matrix[i][j]=0;
  </code></pre>
  
  <p>P.S.: I dropped safety checks , index variables and boundary declarations for clarity of code.</p>
  
  <p>Thanks,<br/>
  Alex<br/></p>