segmentation fault in while loop
<p>I have a program takes inputs from 2 files in c++. Then identify Is the second input file is topological sort? but somehow if i use the list.empty() in a while loop statement, it gives me a segmentation fault, but the for loop doesnt give me any error; however, the for loop only loop once, since i might need to go throught twice.</p>

<pre><code>#include &lt;list&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std ;

list&lt;unsigned&gt; output;

list&lt;unsigned&gt; &amp;
testSort ( istream &amp; idata , istream &amp; sdata )
{
 unsigned n,x1,x2;
 vector&lt; list&lt;unsigned&gt; &gt; successor(n);
 vector&lt;unsigned&gt; count(n,0);
 vector&lt;bool&gt; marks(n,false);

 idata &gt;&gt;n;

 for(int i=0;i&lt;n;i++) {
idata&gt;&gt;x1&gt;&gt;x2;
count[x2]++;
successor[x1].push_back(x2);
if(idata.eof()) break;
 }

 for(int i=0;i&lt;n;i++) {
 sdata&gt;&gt;x1;
  if(count[x1]==0) {
   marks[x1]=true;

   //for(int j=0;j&lt;successor[x1].size();++j) {
    while(!successor[x1].empty()) { 
count[successor[x1].front()]--; 
successor[x1].pop_front();      
  }
 }
  else {
    for(int i=0;i&lt;n;i++)
    {
     if(marks[i]==false)
     output.push_back(i);
    }
   break;
  }  

 }
  return output;
}
</code></pre>
