title: Segfault 11 when initializing large arrays dynamically
body: <p>Hi I'm using C for a school assignment. I have to apologize that I've got very rusty with low-level languages like C. When I try to initialize some arrays dynamically, my code gives me segfault 11 when n&gt;100. I tried to fire up Valgrind to understand what has happened, but I can't quite understand the debug log as the memory block it reports is much less than that I intended to allocate at a large n value. Could anyone kindly help to see what is wrong? Thank you!</p>
  <p>My code (the error happens when I set each entry of the 2D matrix to 1):</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  int n;
  
  typedef struct{
    float x;
    float y;
  } Point2D;
  
  float rand_float(){
      return (float)rand()/RAND_MAX;
  }
  int main(void) {
      srand(time(NULL));
      
      scanf(&quot;%d&quot;, &amp;n);
      
  
      int (*adjm)[n][n] = malloc( sizeof(float[n][n])+1 );
      Point2D (*vcoord)[n] = malloc( sizeof(Point2D[n])+1 ); 
      
      // initialize coords
      for (int i = 0; i &lt; n; i++){
          (*vcoord[i]).x  = rand_float();
          (*vcoord[i]).y  = rand_float();
          printf(&quot;%d %f %f \n&quot;, i, (*vcoord[i]).x, (*vcoord[i]).y);
      }
      for (int x = 0; x &lt; n; x++){
          for (int y = 0; y &lt; n; y++){
               *adjm[x][y] = 1;
          }
      }
      
      // free up memory
      free(*adjm);
      free(*vcoord);
      return 0;
  
  }
  </code></pre>
  <p>First few lines of Valgrind log when I let n=200:</p>
  <pre><code>==18832== Memcheck, a memory error detector
  ==18832== Copyright (C) 2002-2017, and GNU GPLd, by Julian Seward et al.
  ==18832== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
  ==18832== Command: ./out
  ==18832== Parent PID: 1670
  ==18832==
  ==18832== Invalid write of size 4
  ==18832==    at 0x1089F4: main (in /home/usrname/valgrindtest/out)
  ==18832==  Address 0x5257050 is 1,600 bytes inside a block of size 1,601 alloc'd
  ==18832==    at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18832==    by 0x1089B8: main (in /home/usrname/valgrindtest/out)
  ==18832==
  ==18832== Invalid write of size 4
  ==18832==    at 0x108A23: main (in /home/usrname/valgrindtest/out)
  ==18832==  Address 0x5257054 is 3 bytes after a block of size 1,601 alloc'd
  ==18832==    at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  </code></pre>