title: Why does memset put a wrong value?
body: <p>I'm trying to fill zero regions in a matrix using memset() in this way:</p>
  
  <pre><code>unsigned short *ptr;
  for(int i=0; i &lt; nRows; ++i)
  {
      ptr = DepthMat.ptr&lt;unsigned short&gt;(i); /* OCV matrix of uint16 values */
      for(int j=0; j&lt;nCols; ++j)
      {
          uint n=0;
          if(ptr[j] == 0) /* zero region found */
          {
              d_val = ptr[j-1]; /* saves last non-zero value */
              while(ptr[j+n] == 0) 
              { /* looks for non zero */
                  ++n;
              }
              d_val = (d_val &lt; ptr[j+n] ? d_val : ptr[j+n]);
              memset( ptr+j, d_val, n*sizeof( ptr[0]) );
              j += n;
          }
      }
  }
  </code></pre>
  
  <p>I look for sequences of zero, then I store the positions (<code>ptr+j-1</code> and <code>ptr+j+n</code>) and the values of the zero regions boundaries, and finally I use memset() to replace the zeros with d_val.
  The problem is that when I check the values stored they don't match with d_val, for example, I put the value '222' but I get '57054'.
  Any clue?</p>