title: Why does malloc seemingly allow me to write over memory?
body: <p>Why does this not return a segmentation fault 11?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char const *argv[])
  {
      char *test;
      test = (char*) (malloc(sizeof(char)*3));
  
      test = "foo";
      printf("%s\n", test);
  
      test = "foobar";
      printf("%s\n", test);
  
      return 0;
  }
  </code></pre>
  
  <p>My outcome is </p>
  
  <pre><code>foo
  foobar
  </code></pre>
  
  <p>I'm fairly new to C, but when I was compiling this using both gcc on mac and Windows Debugger on Windows 10, it doesn't crash like I expected.</p>
  
  <p>My understanding is that by using <code>(char*) (malloc(sizeof(char)*3))</code>, I am creating a character array of length 3. Then, when I assign test to the string foobar I am writing to 6 array positions.</p>
  
  <p>I'm left sitting here, staring at my apparently valid, runnable code scratching my head.</p>