title: "free(): invalid pointer" error and core dump
body: <p>I got this unwanted output at the end of a successful execution of my program while freeing the dynamic memory that was allocated dynamically.</p>
  
  <pre><code>*** glibc detected *** /home/ahor/Desktop/Project Work/node: free(): invalid pointer: 0xb7fda000 ***
  ======= Backtrace: =========
  /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7e93ee2]
  /home/ahor/Desktop/Project Work/node[0x8048cf9]
  /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e374d3]
  /home/ahor/Desktop/Project Work/node[0x8048561]
  ======= Memory map: ========
  08048000-0804a000 r-xp 00000000 08:06 786473     /home/ahor/Desktop/Project Work/node
  0804a000-0804b000 r--p 00001000 08:06 786473     /home/ahor/Desktop/Project Work/node
  0804b000-0804c000 rw-p 00002000 08:06 786473     /home/ahor/Desktop/Project Work/node
  0804c000-0806d000 rw-p 00000000 00:00 0          [heap]
  b7deb000-b7e07000 r-xp 00000000 08:06 2228253    /lib/i386-linux-gnu/libgcc_s.so.1
  b7e07000-b7e08000 r--p 0001b000 08:06 2228253    /lib/i386-linux-gnu/libgcc_s.so.1
  b7e08000-b7e09000 rw-p 0001c000 08:06 2228253    /lib/i386-linux-gnu/libgcc_s.so.1
  b7e1d000-b7e1e000 rw-p 00000000 00:00 0 
  b7e1e000-b7fc1000 r-xp 00000000 08:06 2228351    /lib/i386-linux-gnu/libc-2.15.so
  b7fc1000-b7fc3000 r--p 001a3000 08:06 2228351    /lib/i386-linux-gnu/libc-2.15.so
  b7fc3000-b7fc4000 rw-p 001a5000 08:06 2228351    /lib/i386-linux-gnu/libc-2.15.so
  b7fc4000-b7fc7000 rw-p 00000000 00:00 0 
  b7fd7000-b7fd8000 rw-p 00000000 00:00 0 
  b7fd8000-b7fd9000 rw-s 00000000 00:04 12517386   /SYSV001120bd (deleted)
  b7fd9000-b7fda000 rw-p 00000000 00:00 0 
  b7fda000-b7fdb000 rw-s 00000000 00:04 12484617   /SYSV00307eff (deleted)
  b7fdb000-b7fdd000 rw-p 00000000 00:00 0 
  b7fdd000-b7fde000 r-xp 00000000 00:00 0          [vdso]
  b7fde000-b7ffe000 r-xp 00000000 08:06 2228363    /lib/i386-linux-gnu/ld-2.15.so
  b7ffe000-b7fff000 r--p 0001f000 08:06 2228363    /lib/i386-linux-gnu/ld-2.15.so
  b7fff000-b8000000 rw-p 00020000 08:06 2228363    /lib/i386-linux-gnu/ld-2.15.so
  bffdf000-c0000000 rw-p 00000000 00:00 0          [stack]
  
  ......
  ......
  ......
  </code></pre>
  
  <p>I recieve this message when I back track using gdb.</p>
  
  <pre><code>Program received signal SIGABRT, Aborted.
  0xb7fdd424 in __kernel_vsyscall ()
  (gdb) bt
  #0  0xb7fdd424 in __kernel_vsyscall ()
  #1  0xb7e4c1df in raise () from /lib/i386-linux-gnu/libc.so.6
  #2  0xb7e4f825 in abort () from /lib/i386-linux-gnu/libc.so.6
  #3  0xb7e8939a in ?? () from /lib/i386-linux-gnu/libc.so.6
  #4  0xb7e93ee2 in ?? () from /lib/i386-linux-gnu/libc.so.6
  #5  0x08048cf9 in main (argc=6, argv=0xbffff214) at node.c:180
  </code></pre>
  
  <p>......
  ......
  Here's my code segment which is affecting this? I cannot detect the cause, I'll be very grateful if you can help me.</p>
  
  <pre><code>// creating the dynamic memory for net
      if((net = (struct dot **)malloc(v * sizeof(struct dot))) == NULL) {
          perror("struct dot malloc() failed\n");
          exit(1);
      }
      for(i = 0 ; i &lt; v; i++)
          if((net[i] = (struct dot *)malloc(v * sizeof(struct dot))) == NULL) {
          perror("net[i] malloc() failed.\n");
          for(j = 0; j &lt; i; j++)
              free(net[j]);
          free(net);      
          }
  .....
  .....
  // creating the dynamic memory for link_status
      if((link_status = (struct link **)malloc(v * sizeof(struct link))) == NULL) {
          perror("struct link malloc() failed\n");
          exit(1);
      }
      for(i = 0 ; i &lt; v; i++)
          if((link_status[i] = (struct link *)malloc(v * sizeof(struct link))) == NULL) {
          perror("link_status[i] malloc() failed.\n");
          for(j = 0; j &lt; i; j++)
              free(link_status[j]);
          free(link_status);      
          }
  ......
  </code></pre>
  
  <p>The glibc detected is from the free() function used below.</p>
  
  <pre><code>// free the allocated dynamic memories
      for(i = 0; i &lt; v; i++) {
          free(net[i]);
          free(link_status[i]);
      }
      free(net);
      free(link_status);
  </code></pre>
  
  <p>Please help me!</p>