2013-02-03 38 views
0

这是我第一篇文章。我是C++的相对新手编程,这个错误让我难堪。程序接收到的信号SIGSEGV,分段错误

我的程序应该从一对空格分隔的文本文件中取出输入并吐出一个csv文件。

我的代码编译就好了,但是,一旦程序崩溃,我已经能够检索以下错误:

正在调试中信号的节目,同时在从GDB调用的函数。 GDB已将上下文恢复到调用之前的状态。 要更改此行为,请使用“set unwindonsignal off”。 包含函数 (std :: string :: size()const)的表达式的评估将被废弃。 编程接收到的信号SIGSEGV,分段故障。 0x00423576在的std :: string ::尺寸()const的()

误差是发生在该代码的第16行:

#include "arrayUtils.h" 
#include "enrollment.h" 
#include <string> 
#include <iostream> 

using namespace std; 


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

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

    courseFile >> ws; 

    courseFile >> 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& enrollmentRequestsFile, 
       int numCourses, 
       string* courseNames, 
       int* maxEnrollments, 
       int* enrollments) 
{ 
    // Start the enrollment counters at zero 
    for (int pos = 0; pos < numCourses; ++pos) 
    { 
     enrollments[pos] = 0; 
    } 

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

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

    courseIndex = binarySearch(courseNames, numCourses, courseName); 

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

    enrollmentRequestsFile >> courseName; 
    } 
} 


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


void processEnrollments (istream& courseFile, istream& enrollmentRequestsFile, 
      ostream& reportFile) 
{ 
    int numCourses = 0; 
    int arraySize = 0; 
    courseFile >> 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); 
} 

,我打电话来组织字符串数组的功能是:

// 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 <typename T> 
int addInOrder (T* array, int& size, T value) 
{ 
    // Make room for the insertion 
    int toBeMoved = size - 1; 
    while (toBeMoved >= 0 && value < array[toBeMoved]) { 
    array[toBeMoved+1] = array[toBeMoved]; 
    --toBeMoved; 
    } 
    // Insert the new value 
    array[toBeMoved+1] = value; 
    ++size; 
    return toBeMoved+1; 
} 

请帮忙!我不知道该如何解决这个问题,该计划即将到期!

编辑:

主要程序是这样的:

#include <cstdlib> 
#include <iostream> 
#include <string> 
#include <fstream> 

#include "enrollment.h" 


using namespace std; 


int main (int argc, char** argv) 
{ 
    if (argc != 4) 
    { 
     cerr << "Usage: " << argv[0] << " courseFile enrollmentFile reportFile" << 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; 
} 
+0

最明显的怀疑是你正在写入超出数组或分配内存的范围。如何将参数'string * courseNames'传递给分配给'readCourses()'的参数? –

+1

toBeMoved初始化为value(size-1),所以第一次通过循环写入array [toBeMoved + 1],又名array [(size-1)+1],又名array [size],它是一个超过数组的末尾,这是未定义的行为,并可能导致崩溃。 –

+0

我在声明并初始化时收到了第16行的分段错误: string courseNameValue =“”; //问题从这里开始 int maxEnrollmentValue = 0; 在这一点上,我还没有开始任何循环。 – LTDAN626

回答

1

在addInOrder功能:

int toBeMoved = size - 1; 
while (toBeMoved >= 0 && value < array[toBeMoved]) { 
    array[toBeMoved+1] = array[toBeMoved]; 

toBeMoved成为大小在这里第一次迭代中,出的数组边界。

+0

所以我应该增加原始数组大小来解决这个问题?我不知道我应该怎么做来纠正这个问题。 – LTDAN626

+0

我希望我可以,但addInOrder函数是必需的分配。 addInOrder函数是由我的教授提供的,所以我知道它确实有效(很多人的程序都使用这个函数)。我只需要弄清楚我哪里出错了。 – LTDAN626

+0

@ LTDAN626快速问题,你如何运行可执行文件? –

0

扩展在杰梅因许的回答:

解决方案:

使用std::vector<T>,而不是一个数组/指针。它需要照顾动态调整大小和删除..您的程序也将更清洁(也可能更高效,以及)

+0

我希望我可以,但addInOrder函数是必需的分配。 addInOrder函数是由我的教授提供的,所以我知道它确实有效(很多人的程序都使用这个函数)。我只需要弄清楚我哪里出错了。 – LTDAN626

0

您没有正确使用addInOrder功能。

string courseNames[numCourses + 1]; 

courseNames阵列如上所定义,然后传递courseNamenumCoursesaddInOrder直接。在addInOrder你这样做:

courseName[numCourses] //toBeMoved = size - 1; toBeMoved+1 

您访问了courseName数组边界的,你其实是要访问last element of courseName

所以,你需要一个额外的变量来记住你当前的数组大小。这样的事情:

void readCourses (istream& courseFile, int numCourses, 
      string* courseNames, int* maxEnrollments) 
{ 
    string courseNameValue = ""; 
    int maxEnrollmentValue = 0; 

    int index = 0;   // index to how many real courses are added to courseName array 
    while (courseFile){ 
    if (index == numCourses){ // courseName array is full, break out 
     break; 
    } 
    courseFile >> courseNameValue; 
    addInOrder(courseNames, index , courseNameValue); // pass in index, not array size 

    courseFile >> ws; 

    courseFile >> maxEnrollmentValue; 
    addInOrder(maxEnrollments, numCourses, maxEnrollmentValue); 
    } 
} 

希望这有助于!

相关问题