title: Segmentation Fault, program works fine
body: <p>I'm implementing the Prim's Algorithm for Minimum Spanning Tree Problem. Program Just works fine and I get the desired output but I received a <em>Segmentation Fault</em> in the End. I checked my program for errors but the program is fine.    </p>
  
  <p>Here's <strong>Source Code</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdio&gt;
  #define INF 1000000
  #define N 5
  
  using namespace std;
  
  class Graph
  {
      int V;
      int adj[][N];
  
      public:
          Graph(int,int mat[][N]);
          void primsMST();
  };
  
  Graph::Graph(int v, int mat[][N])
  {
     this-&gt;V = v;
     for(int i=0; i&lt;v; i++)
     {
         for(int j=0; j&lt;v; j++)
             adj[i][j] = mat[i][j];
     }
  }
  
  void Graph::primsMST()
  {
      int i, j;
  
      int key[V];     // Key Value which is used to pick minimum weight vertex
      int parent[V];  // To store Tree
      bool mstSet[V];  // Mark Vertex included in MST
  
      // Initialize Key Values to infinite and mstSet to false
      for(i=0; i&lt;V; i++)
      {
          key[i]=INF;
          mstSet[i] = false;
      }
  
      // Initialize initial vertex
      key[0] = 0;
      parent[0] = -1;
  
      int minm, min_i;
      for(int k=0; k&lt;V-1; k++)
      {
          // Get min Key from all vertices
          minm = INF;
          for(i=0; i&lt;V; i++) {
              if(!mstSet[i] &amp;&amp; key[i]&lt;minm)
              {
                  minm = key[i];
                  min_i = i;
              }
          }
  
          // Include min key vertex in MST
          mstSet[min_i] = true;
  
          // Update key values of vertices adjacent to min vertex
          for(j=0; j&lt;V; j++)
          {
              if( adj[min_i][j] &amp;&amp; !mstSet[j] &amp;&amp; adj[min_i][j] &lt; key[j])
              {
                  key[j] = adj[min_i][j];
                  parent[j] = min_i;
              }
          }
      }
  
      for(i=0; i&lt;V; i++)
          cout&lt;&lt;i&lt;&lt;", "&lt;&lt;key[i]&lt;&lt;", "&lt;&lt;parent[i]&lt;&lt;endl;
  
      // Print Minimum spanning Tree
      cout&lt;&lt;"Edge\tWeight"&lt;&lt;endl;
      for(i=1; i&lt;V; i++)
          cout&lt;&lt;i&lt;&lt;"--"&lt;&lt;parent[i]&lt;&lt;"\t"&lt;&lt;adj[i][parent[i]]&lt;&lt;endl;
  
      cout&lt;&lt;endl;
  }
  
  int main()
  {
  
      int adjmat[][N] =  {{0, 2, 0, 6, 0},
                        {2, 0, 3, 8, 5},
                        {0, 3, 0, 0, 7},
                        {6, 8, 0, 0, 9},
                        {0, 5, 7, 9, 0},
                       };
  
      Graph g(N, adjmat);
  
      g.primsMST();
  
      cout&lt;&lt;endl;
      return 0;
  }
  </code></pre>
  
  <p>Program is compiled using <code>gcc version 4.7.2</code>.
  When I run this program I got the desired output for the Graph but a Segmentation fault occurs in the end.  </p>
  
  <pre><code>$ ./primsMST
  0, 0, -1
  1, 2, 0
  2, 3, 1
  3, 6, 0
  4, 5, 1
  Edge    Weight
  1--0    2
  2--1    3
  3--0    6
  4--1    5
  
  Segmentation fault
  </code></pre>
  
  <p>Here's the debugging output using <strong>gdb</strong>  </p>
  
  <pre><code>$ gdb primsMST
  GNU gdb (GDB) 7.4.1-debian
  Copyright (C) 2012 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  and "show warranty" for details.
  This GDB was configured as "x86_64-linux-gnu".
  For bug reporting instructions, please see:
  &lt;http://www.gnu.org/software/gdb/bugs/&gt;...
  Reading symbols from /home/abhishek/myPrograms/geeksforgeeks/graphs/primsMST...done.
  (gdb) set logging on
  Copying output to gdb.txt.
  (gdb) run
  Starting program: /home/abhishek/myPrograms/geeksforgeeks/graphs/primsMST 
  0, 0, -1
  1, 2, 0
  2, 3, 1
  3, 6, 0
  4, 5, 1
  Edge    Weight
  1--0    2
  2--1    3
  3--0    6
  4--1    5
  
  
  
  Program received signal SIGSEGV, Segmentation fault.
  0x0000000000000002 in ?? ()
  (gdb) backtrace
  #0  0x0000000000000002 in ?? ()
  #1  0x0000000800000003 in ?? ()
  #2  0x0000000000000005 in ?? ()
  #3  0x0000000000000003 in ?? ()
  #4  0x0000000700000000 in ?? ()
  #5  0x0000000800000006 in ?? ()
  #6  0x0000000000000000 in ?? ()
  (gdb) quit 
  </code></pre>
  
  <p><strong>Can anyone explain why I'm getting this Error?</strong></p>