這是我第一篇文章。我是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;
}
最明顯的懷疑是你正在寫入超出數組或分配內存的範圍。如何將參數'string * courseNames'傳遞給分配給'readCourses()'的參數? –
toBeMoved初始化爲value(size-1),所以第一次通過循環寫入array [toBeMoved + 1],又名array [(size-1)+1],又名array [size],它是一個超過數組的末尾,這是未定義的行爲,並可能導致崩潰。 –
我在聲明並初始化時收到了第16行的分段錯誤: string courseNameValue =「」; //問題從這裏開始 int maxEnrollmentValue = 0; 在這一點上,我還沒有開始任何循環。 – LTDAN626