title: Strange behavior when assigning values to a matrix (C/C++)
body: <p>I am allocating three matrix (<em>matR</em>, <em>matG</em>, <em>matB</em>) using malloc and assigning to them some values of the vector <em>image</em>. 
  As usual, i am using two integers (i, j) to make the loop for this job. In the first time, when i=0 and j=0, it assigns the following values: 197 to matR[0][0], 211 to matG[0][0] and 219 to matB[0][0]. Until now is OK. Those are the values that i want. But here comes the strange behavior: in some points new values are assigned to matR[0][0], matG[0][0], matB[0][0]!</p>
  
  <p>This is the part of the code that is doing what i said:</p>
  
  <pre><code>int i, j, k;
  std::vector&lt;unsigned char&gt; image; //the raw pixels
  unsigned error = lodepng::decode(image, width, height, filename);
  
  matR = (unsigned char**)malloc(sizeof(unsigned char)*width);
  matG = (unsigned char**)malloc(sizeof(unsigned char)*width);
  matB = (unsigned char**)malloc(sizeof(unsigned char)*width);
  
  k=0;
  for(i=0; i&lt;width; i++) {
    matR[i] = (unsigned char*)malloc(sizeof(unsigned char)*height);
    matG[i] = (unsigned char*)malloc(sizeof(unsigned char)*height);
    matB[i] = (unsigned char*)malloc(sizeof(unsigned char)*height);
  
    for(j=0; j&lt;height; j++) {
      matR[i][j] = image[k++];
      matG[i][j] = image[k++];
      matB[i][j] = image[k++];
      k++; //separator
  }
  </code></pre>
  
  <p>I added some lines to debbug the code and, running on terminal, i got the following output:
  <a href="http://imgur.com/XvQIGYk" rel="nofollow">http://imgur.com/XvQIGYk</a></p>
  
  <p>*It shows that when i=100 and j=0, the value of matR[0][0] was changed to 176.</p>
  
  <p>What is going on?</p>