title: Invalid pointer in C program reading file
body: <p>I have a strange problem when running a C program in Linux.
  It compiles without any errors, but when I run it I get this error message:</p>
  
  <pre><code>*** Error in `./catter': munmap_chunk(): invalid pointer: 0x00007fffc7953310 ***
  ======= Backtrace: =========
  /lib64/libc.so.6[0x3ee787bc17]
  ./catter[0x4007d3]
  /lib64/libc.so.6(__libc_start_main+0xf5)[0x3ee7821b75]
  ./catter[0x400589]
  ======= Memory map: ========
  00400000-00401000 r-xp 00000000 08:09 12200149                           /home/sasha/catter
  00600000-00601000 r--p 00000000 08:09 12200149                           /home/sasha/catter
  00601000-00602000 rw-p 00001000 08:09 12200149                           /home/sasha/catter
  01301000-01322000 rw-p 00000000 00:00 0                                  [heap]
  30a8200000-30a8215000 r-xp 00000000 08:08 795604                         /usr/lib64/libgcc_s-4.8.1-20130603.so.1
  30a8215000-30a8414000 ---p 00015000 08:08 795604                         /usr/lib64/libgcc_s-4.8.1-20130603.so.1
  30a8414000-30a8415000 r--p 00014000 08:08 795604                         /usr/lib64/libgcc_s-4.8.1-20130603.so.1
  30a8415000-30a8416000 rw-p 00015000 08:08 795604                         /usr/lib64/libgcc_s-4.8.1-20130603.so.1
  3ee7400000-3ee7421000 r-xp 00000000 08:08 786482                         /usr/lib64/ld-2.17.so
  3ee7620000-3ee7621000 r--p 00020000 08:08 786482                         /usr/lib64/ld-2.17.so
  3ee7621000-3ee7622000 rw-p 00021000 08:08 786482                         /usr/lib64/ld-2.17.so
  3ee7622000-3ee7623000 rw-p 00000000 00:00 0 
  3ee7800000-3ee79b6000 r-xp 00000000 08:08 786735                         /usr/lib64/libc-2.17.so
  3ee79b6000-3ee7bb6000 ---p 001b6000 08:08 786735                         /usr/lib64/libc-2.17.so
  3ee7bb6000-3ee7bba000 r--p 001b6000 08:08 786735                         /usr/lib64/libc-2.17.so
  3ee7bba000-3ee7bbc000 rw-p 001ba000 08:08 786735                         /usr/lib64/libc-2.17.so
  3ee7bbc000-3ee7bc1000 rw-p 00000000 00:00 0 
  7f997a36e000-7f997a371000 rw-p 00000000 00:00 0 
  7f997a392000-7f997a395000 rw-p 00000000 00:00 0 
  7fffc7935000-7fffc7956000 rw-p 00000000 00:00 0                          [stack]
  7fffc795c000-7fffc795e000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  Aborted
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  int FILEERROR_ = 0;
  char *ReadFile(char *filename, char *permissions)
  {
      char output[FileSize(filename) + 1];//1 for a start
      char c;
      FILE *fp;
      int i = 0;
      fp = fopen(filename, permissions);
      while((c = fgetc(fp)) != EOF)
      {
          output[i] = c;
          i++;
      }
      fclose(fp);
      char *allo = &amp;output[0];
      return allo;
  }
  int FileSize(char *filename)
  {
      int i = 0;
      FILE *fp;
      fp = fopen(filename, "r");
      while(fgetc(fp) != EOF)
      {       
          i++;
      }
      fclose(fp);
      return i;
  }
  int main()
  {
      char *read = ReadFile("a.out", "r");
      printf("%s", read);
      free(read);
      return 0;
  }
  </code></pre>
  
  <p>If this is relevant, I am using Fedora Linux, 64-bit with a GCC C Compiler.</p>