title: Windows 7 64 bit c++ malloc fail
body: <p>I have a small <strong>c++</strong> programme that fails to allocate more than 2880 with <code>malloc</code>. The statement:</p>
  
  <pre><code>void* tmpptr = malloc(2881);
  </code></pre>
  
  <p>crashes, whilst </p>
  
  <pre><code>void* tmpptr = malloc(2880);
  </code></pre>
  
  <p>does <strong>not</strong>. Every time!</p>
  
  <p>I am using MinGW and compiling with</p>
  
  <pre><code>g++ -std=c++0x -pedantic -Wall -Wextra -g -D_GLIBCXX_DEBUG -static-libgcc -static-libstdc++
  </code></pre>
  
  <p>I know that the use of <code>malloc</code> is discouraged in C++ and I am planning to rewrite this anyway, but I would still want to know why this is not working. The same code has been working when I compiled it with gcc. </p>
  
  <p>Update:
  This is the main function calls:</p>
  
  <pre><code>image * img = readPPM("pic/pic.ppm");
  bw_image * sky = skyline(img, ref);
  cont * lin = contour(sky, 0); // &lt;-- chash
  ...
  </code></pre>
  
  <p>And the function starts with:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  cont * contour(const bw_image * img, const char wrap) {
      int test = 2880;
      void* ptr1 = malloc(test);
      void* ptr2 = malloc(test);
  ...
  </code></pre>
  
  <p>Now the first <code>malloc</code> will work but not the second one. If I change <code>test = 1440;</code>, same result. But; for <code>test = 140;</code> already the first <code>malloc</code> will fail.</p>
  
  <p>I've tried the code as a stand alone:</p>
  
  <pre><code>int main(int argc, char *argv[]) {
      int size = 2881;
      void* tmpptr;
      printf("Allocating, %d\n", size);
      tmpptr = malloc(size);
      printf("Allocated %d bytes successfully\n", size);
  }
  </code></pre>
  
  <p>and it works without problems, so it seems to be something in <code>main</code> doing it.</p>
  
  <p><code>rem_artifacts</code> look like this</p>
  
  <pre><code>void rem_artifacts(bw_image * sky) {
      for (int y = 0; y &lt; sky-&gt;y; ++y) for (int x = 0; x &lt; sky-&gt;x; ++x) {
          int xp = x - 1, xn = x + 1;
          if (xp &lt; 0) xp = sky-&gt;x - 1;
          if (xn == sky-&gt;x) xn = 0;
          int c = sky-&gt;data[x][y];  // this is wrong
          if (
              (y == 0 || sky-&gt;data[x][y-1] != c) &amp;&amp;  // and this
              (y == sky-&gt;y-1 || sky-&gt;data[x][y+1] != c) &amp;&amp; // and this
              sky-&gt;data[xp][y] != c &amp;&amp;  // and this
              sky-&gt;data[xn][y] !=c  // and this
          ) sky-&gt;data[x][y] = !c; // and this
      }
  }
  bw_image * skyline(const image * img, const image * ref) {
      double tilt = 114.0 - 90.0;
      double pi = 3.14159265358979323846;
      double chang = 360.0 / 2.0 / pi;
      //double sint = sin(tilt / chang);
      //double cost = cos(tilt / chang);
      bw_image * sky = (bw_image*)malloc(sizeof(bw_image));
      sky-&gt;x = img-&gt;x;
      sky-&gt;y = img-&gt;y;    //  
      double cos30 = sqrt(3)/2;
      int lim0 = (int)((double)(img-&gt;y) / 2.0 + (double)(img-&gt;x) * tan(tilt/chang) * cos30);
      sky-&gt;data = (char**)malloc(sizeof(char*) * sky-&gt;y);
      for (int y = 0; y &lt; sky-&gt;y; ++y) {
          sky-&gt;data[y] = (char*)malloc(sizeof(char) * sky-&gt;x);
          for (int x = 0; x &lt; sky-&gt;x; ++x)
              sky-&gt;data[y][x] = !(y &lt; lim0 &amp;&amp; colour_dist_sq(img-&gt;data[y][x], ref-&gt;data[y][x]) &lt; 5000.0);
      }
      rem_artifacts(sky);
      return sky;
  }
  </code></pre>