title: Access violation after malloc
body: <p>Could someone please help me understand the following behaviour:</p>
  
  <p>I have a little piece of code for cloning a float image.
  The method <code>Clone</code> takes a pointer to another image and its dimensions as arguments.</p>
  
  <p>Everything works as expected, but sometimes this line <code>clone[i] = color;</code> causes an Access Violation. The occurrence of the exception is not predictable neither periodic. Inspecting variables at crash time shows that <code>Color color = source[i];</code> is always set and valid.</p>
  
  <p>How is it possible that <code>malloc</code> returns a bad pointer?</p>
  
  <p>The code:</p>
  
  <pre><code>typedef struct
  {
      float r;
      float g;
      float b;
      float a;
  } Color;
  
  Color* Clone(Color* source, int width, int height)
  {
      int s = width * height;
  
      Color *clone;
      clone = (Color *)malloc(s * sizeof(Color));
  
      if (clone)
      {
          for (int i = 0; i &lt; s; i++)
          {
              Color color = source[i];
  
              // Sometimes app crash here: Access violation
              clone[i] = color;
          }
      }
  
      return clone;
  }
  </code></pre>
  
  <p>Any help is much appreciated.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Platform: Windows 64bit</p>
  
  <p>Values of variables at crash time:</p>
  
  <pre><code>width = 256
  height = 256
  s = 655536
  i = 0
  </code></pre>