title: Segmentation fault in File I/O
body: <p>I have written a code to read a file, store it in a structure and just display it. But somehow it is giving me a segmentation fault and I dont know why. Can someone please help me?</p>
  
  <p>Output:</p>
  
  <pre><code>file: /home/neel/map2.txt
  file opened
  Start Intersection
  a-&gt;road: 4
  
  a-&gt;roadId[0]: 1
  a-&gt;lane[0][0]: 2
  a-&gt;lane[0][1]: 2
  
  a-&gt;roadId[1]: 2
  a-&gt;lane[1][0]: 2
  a-&gt;lane[1][1]: 2
  
  a-&gt;roadId[2]: 3
  Segmentation fault
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;stdio.h&gt;
  using namespace std;
  
  struct Intersection
  {
    unsigned short road;
    long long int *roadId;
    short *lane[2];
  };
  
  int main(int argc, char** argv)
  {
    std::ifstream file;
    cout&lt;&lt;"file: "&lt;&lt;argv[1]&lt;&lt;endl;
    file.open(argv[1], std::ios::in);
    cout&lt;&lt;"file opened"&lt;&lt;endl;
  
    while (!file.eof())
    {
      cout&lt;&lt;"Start Intersection"&lt;&lt;endl;
      Intersection *a = new Intersection;
      file&gt;&gt;a-&gt;road;
      a-&gt;roadId = new long long int[a-&gt;road];
      a-&gt;lane[0] = new short[a-&gt;road];
      a-&gt;lane[1] = new short[a-&gt;road];
      cout&lt;&lt;"a-&gt;road: "&lt;&lt;a-&gt;road&lt;&lt;endl;
      for (int i=0; i&lt;a-&gt;road; i++)
      {
        file&gt;&gt;a-&gt;roadId[i];
  
        cout&lt;&lt;endl&lt;&lt;"a-&gt;roadId["&lt;&lt;i&lt;&lt;"]: "&lt;&lt;a-&gt;roadId[i]&lt;&lt;endl;
        file&gt;&gt;a-&gt;lane[i][0];
        cout&lt;&lt;"a-&gt;lane["&lt;&lt;i&lt;&lt;"][0]: "&lt;&lt;a-&gt;lane[i][0]&lt;&lt;endl;
        file&gt;&gt;a-&gt;lane[i][1];
        cout&lt;&lt;"a-&gt;lane["&lt;&lt;i&lt;&lt;"][1]: "&lt;&lt;a-&gt;lane[i][1]&lt;&lt;endl;
      }
      cout&lt;&lt;"Intersection inserted"&lt;&lt;endl;
      delete a;
    }
  }
  </code></pre>
  
  <p>Text file:</p>
  
  <pre><code>4
  
  1
  2
  2
  
  2
  2
  2
  
  3
  2
  2
  
  4
  2
  2
  </code></pre>