Program received signal SIGSEGV, Segmentation fault
<p>This is my first post here.  I am a relative novice programming in C++ and this error has me stumped.</p>

<p>My program is supposed to take input from a couple of space delimited text files and spit out a csv file.</p>

<p>My code compiles just fine, however, the program crashes and I have been able to retrieve the following error:</p>

<p>The program being debugged was signaled while in a function called from GDB.
GDB has restored the context to what it was before the call.
To change this behavior use "set unwindonsignal off".
Evaluation of the expression containing the function
(std::string::size() const) will be abandoned.
Program received signal SIGSEGV, Segmentation fault.
0x00423576 in std::string::size() const ()</p>

<p>The error is occuring on line 16 of this code:</p>

<pre><code>#include "arrayUtils.h"
#include "enrollment.h"
#include &lt;string&gt;
#include &lt;iostream&gt;

using namespace std;


/*
 * Read the course names and max enrollments. Keep the courses
 * in alphabetic order by course name.
 */
void readCourses (istream&amp; courseFile, int numCourses,
          string* courseNames, int* maxEnrollments)
{
  string courseNameValue = ""; // PROBLEMS START HERE
  int maxEnrollmentValue = 0;

  while (courseFile){
    courseFile &gt;&gt; courseNameValue;
    addInOrder(courseNames, numCourses, courseNameValue); //PROGRAM CRASHES HERE

    courseFile &gt;&gt; ws;

    courseFile &gt;&gt; maxEnrollmentValue;
    addInOrder(maxEnrollments, numCourses, maxEnrollmentValue);
  }
}

/*
 * Read the enrollment requests, processing each one and tracking
 * the number of students successfully enrolled into each course.
 */
void processEnrollmentRequests (istream&amp; enrollmentRequestsFile,
                int numCourses,
                string* courseNames,
                int* maxEnrollments,
                int* enrollments)
{
  // Start the enrollment counters at zero
  for (int pos = 0; pos &lt; numCourses; ++pos)
  {
      enrollments[pos] = 0;
  }

  // Read the requests, one at a time, serving each one
  string courseName;
  int courseIndex = 0;

  enrollmentRequestsFile &gt;&gt; courseName;
  while (enrollmentRequestsFile) {
    enrollmentRequestsFile &gt;&gt; ws;
    string studentName;
    getline (enrollmentRequestsFile, studentName);

    courseIndex = binarySearch(courseNames, numCourses, courseName);

    if (courseIndex &gt;= 0)
    {
        if (maxEnrollments[courseIndex] &gt;= enrollments[courseIndex])
        {
            ++enrollments[courseIndex];
            cout &lt;&lt; studentName &lt;&lt; " has enrolled in " &lt;&lt; courseName &lt;&lt; "\n";
        }
        else
        {
            cout &lt;&lt; studentName &lt;&lt; " cannot be enrolled in " &lt;&lt; courseName &lt;&lt; "\n";
        }
    }
    else
    {
        cout &lt;&lt; studentName &lt;&lt; " cannot be enrolled in " &lt;&lt; courseName &lt;&lt; "\n";
    }

    enrollmentRequestsFile &gt;&gt; courseName;
   }
}


/*
 * Write a CSV report listing each course and its enrollment.
 */
void generateReport (ostream&amp; reportFile,
             int numCourses,
             string* courseNames,
             int* enrollments)
{
  for (int pos = 0; pos &lt; numCourses; ++pos)
  {
      reportFile &lt;&lt; "\"" &lt;&lt; courseNames[pos] &lt;&lt; "\"," &lt;&lt; enrollments &lt;&lt; "\n";
  }
}


void processEnrollments (istream&amp; courseFile, istream&amp; enrollmentRequestsFile,
             ostream&amp; reportFile)
{ 
  int numCourses = 0;
  int arraySize = 0;
  courseFile &gt;&gt; numCourses;

  arraySize = numCourses + 1;

  // Create the arrays we need
  string courseNames[arraySize];
  int maxEnrollments[arraySize];
  int enrollments[arraySize];

  // Process the enrollments
  readCourses (courseFile, numCourses, courseNames, maxEnrollments);
  processEnrollmentRequests (enrollmentRequestsFile, numCourses,
                 courseNames, maxEnrollments, enrollments);
  generateReport (reportFile, numCourses, courseNames, enrollments);
}
</code></pre>

<p>The function that I am calling to organize the string array is:</p>

<pre><code>// Assume the elements of the array are already in order
// Find the position where value could be added to keep
//    everything in order, and insert it there.
// Return the position where it was inserted
//  - Assumes that we have a separate integer (size) indicating how
//     many elements are in the array
//  - and that the "true" size of the array is at least one larger
//      than the current value of that counter
template &lt;typename T&gt;
int addInOrder (T* array, int&amp; size, T value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved &gt;= 0 &amp;&amp; value &lt; array[toBeMoved]) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[toBeMoved+1] = value;
  ++size;
  return toBeMoved+1;
}
</code></pre>

<p>Please help!  I don't know what to do to fix this and the program is due soon!</p>

<p>EDIT:</p>

<p>The main program looks like this:</p>

<pre><code>#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;

#include "enrollment.h"


using namespace std;


int main (int argc, char** argv)
{
  if (argc != 4)
    {
      cerr &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " courseFile enrollmentFile reportFile" &lt;&lt; endl;
      return -1;
    }

  // Take input and output file names from the command line
  ifstream coursesIn (argv[1]);
  ifstream enrollmentIn (argv[2]);
  ofstream reportOut (argv[3]);

  processEnrollments (coursesIn, enrollmentIn, reportOut);

  coursesIn.close();
  enrollmentIn.close();
  reportOut.close();

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