title: Feeding in data from a file to a dynamically allocated array in C++
body: <p>So, I'm doing a college assignment where basically what is needed is that you use a pointer that points to a dynamically allocated array of the correct size based on input from a data file. The problem is, I keep getting different errors when the program runs. The program compiles perfectly fine, but yet when it runs, it gives me this: &quot;0xC0000005: Access violation reading location 0xFDFDFDFD.&quot;</p>
  <p>Here is the code I'm using (do note that some of the code was written by my professor and before you ask, I attempted to contact him, and never got anything back from him):</p>
  <blockquote>
  <p>Written by Professor</p>
  </blockquote>
  <pre><code>struct emprec   /*Employee record with two fields*/
  {
      int id;
      float salary;
  };
  typedef emprec* emprecptr; /*Employee record pointer type*/
  typedef emprecptr* indexarr; /* Pointer to a dynamic array of employee record pointers*/
  </code></pre>
  <blockquote>
  <p>Written by me</p>
  </blockquote>
  <pre><code>void readdata(indexarr&amp; ptrs, int&amp; size)
  {
      size = 0;
  
      ptrs = nullptr;
      ptrs = new emprecptr[size];
      ifstream in;
      in.open(&quot;p1data.txt&quot;);
      if (!in) {
          cout &lt;&lt; &quot;Unable to open p1data.txt&quot;; //if the file is not in the folder
          exit(1);
      }
      while (!in.eof()) {
          in &gt;&gt; ptrs[size]-&gt;id;        //putting to read line for id
          in.ignore();
          in &gt;&gt; ptrs[size]-&gt;salary;       //putting to read line for salary
          in.ignore();
          size = size + 1;
  
      }
      in.close();
  };
  
  </code></pre>