2013-02-03 43 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); 
    } 
} 

希望這有助於!

相關問題