title: I can access memory that I'm not supposed to be accessing
body: <p>So I was playing around with pointers and malloc and while I was doing it I got completely confused by my code working when it's not supposed to.(I think?)
  Here is my code:</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void test1() {
    int arr[10];
  
    for (int i = 0; i &lt; 20; i++) {
      printf(&quot;%i &quot;, arr[i]);
    }
  
    printf(&quot;\n&quot;);
  }
  
  void test2() {
    char *thing = (char *)malloc(10 * sizeof(char));
  
    for (int i = 0; i &lt; 25; i++) {
      thing[i] = 'A';
    }
  
    for (int i = 0; i &lt; 30; i++) {
      printf(&quot;%c &quot;, thing[i]);
    }
  
    thing[10] = 'B';
  
    printf(&quot;%c\n&quot;, thing[10]);
  }
  
  int main() {
    // test1();
    test2();
  
    return 0;
  }
  </code></pre>
  <p>In test2, I think I am allocating 10 blocks of memory of size char or 10 bytes of memory.
  But somehow <code>thing[i]</code> can be written to, for <code>i</code> ranging between 0 all the way to 25.</p>
  <p>I then try to print 30 elements starting from pointer <code>thing</code> and i successfully manage to do so.</p>
  <p>On output I get this</p>
  <pre><code>A A A A A A A A A A A A A A A A A A A A A A A A       B
  </code></pre>
  <p>My question is:</p>
  <p>Why was I able to write and read 25 A's when I only allocated 10 bytes of memory?</p>
  <p>Thanks in advance!</p>