【问题标题】:Segmentation fault in class definitions cannot figure out where类定义中的分段错误无法确定在哪里
【发布时间】:2013-10-11 02:03:26
【问题描述】:

我在过去 4 小时内一直试图找到一个分段错误。以下是 valgrind 可能使用的代码和一些信息。

  Course::Course()
{
    courseid = "None";
    courseName = "None";
    enrolled[2] = {0};

}

Course::Course(string courseNum, string coursename, string filename)
{
    courseid = courseNum;
    courseName = coursename;
    enrolled[2] = {0};
    readStudentData(filename);
    sortRow(0);
    sortRow(1);
    sortRow(2);

}


void Course::readStudentData(string filename)
{
    std::ifstream inFile; //declare file input stream object
    string first, last, zid;
    int section;

    inFile.open(filename.c_str()); //open file 
    if (!inFile)
    {
        cout << "File did not open successfully!"; //error checking
        exit(EXIT_FAILURE);
    }

    inFile >> first; //read first
    while (inFile)
    {
        inFile >> last;
        inFile >> zid;
        inFile >> section;
        section--; //match section with array subscript

        Student newStudent(last, first, zid);   //fill  contents read to new student object



        Students[section][enrolled[section]] = newStudent; //fill Students array with new student 

        enrolled[section]++; //add the student to the enrolled counter

        inFile >> first;
    }

    inFile.close();
}


void Course::sortRow(int RowtoSort)
{
    int i, j;
    Student bucket;

    for(i = 1; i < enrolled[RowtoSort];i++)
    {
        bucket = Students[RowtoSort][i]; //assign bucket with temp value for swap


            for(j = i; (j > 0) && (Students[RowtoSort][j-1].getLastName().compare(bucket.getLastName())) < 0; j--)
        {
            Students[RowtoSort][j] = Students[RowtoSort][j-1]; //assign j to element j-1 so now both j and j-1 are the same in the array
        }
        Students[RowtoSort][j] = bucket; //now j-1 value is at j and since j-- assign it to bucket to place the proper value at j-1  

    } // end outer loop

}


void Course::print()
{
    cout << courseid << " " << courseName << endl; //header

    cout << "Section 1" << endl; //print section 1
    cout << left << "Name" << setw(40) << "Z-id" << endl;

    for(int i=0;i<enrolled[0];i++)
    {
        Students[0][i].print();          
    }

    cout << "Section 2" << endl;    //print section 2
    cout << left << "Name" << setw(40) << "Z-id" << endl;
    for(int i=0;i<enrolled[0];i++)
    {
        Students[1][i].print();   
    }

    cout << "Section 3" << endl;    //print section 3
    cout << left << "Name" << setw(40) << "Z-id" << endl;
    for(int i=0;i<enrolled[0];i++)
    {
        Students[2][i].print();   
    }


}

这是 valgrind 提供的信息

==4223== Use of uninitialised value of size 8
==4223==    at 0x4EF15F1: std::string::assign(std::string const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==4223==    by 0x4023E6: Student::operator=(Student const&) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401B9F: Course::readStudentData(std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401836: Course::Course(std::string, std::string, std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x4012F9: main (assign3.cpp:27)
==4223== 
==4223== Use of uninitialised value of size 8
==4223==    at 0x4EF15F1: std::string::assign(std::string const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==4223==    by 0x402401: Student::operator=(Student const&) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401B9F: Course::readStudentData(std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401836: Course::Course(std::string, std::string, std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x4012F9: main (assign3.cpp:27)
==4223== 
==4223== Use of uninitialised value of size 8
==4223==    at 0x4EF15F1: std::string::assign(std::string const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==4223==    by 0x40241C: Student::operator=(Student const&) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401B9F: Course::readStudentData(std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401836: Course::Course(std::string, std::string, std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x4012F9: main (assign3.cpp:27)
==4223== 
==4223== Invalid read of size 8
==4223==    at 0x4EF15F1: std::string::assign(std::string const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==4223==    by 0x4023E6: Student::operator=(Student const&) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401B9F: Course::readStudentData(std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401836: Course::Course(std::string, std::string, std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x4012F9: main (assign3.cpp:27)
==4223==  Address 0x8050190f0 is not stack'd, malloc'd or (recently) free'd
==4223== 
==4223== 
==4223== Process terminating with default action of signal 11 (SIGSEGV)
==4223==  Access not within mapped region at address 0x8050190F0
==4223==    at 0x4EF15F1: std::string::assign(std::string const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==4223==    by 0x4023E6: Student::operator=(Student const&) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401B9F: Course::readStudentData(std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x401836: Course::Course(std::string, std::string, std::string) (in /home/ruslan/241Assign3/assign3)
==4223==    by 0x4012F9: main (assign3.cpp:27)

我有另一个学生课程已被遵守并经过测试可以正常工作,因此错误一定在课程中。

课程头文件:

#ifndef COURSE_H
#define COURSE_H

#include "student.h"

using std::string;

    class Course
    {
        private:
        string courseid;
        string courseName;
        Student Students[3][45];
        int enrolled[2];

        public:
        Course(); //default constructor
        Course(string, string, string); //alternate constructor 
        void readStudentData(string);
        void sortRow(int);
        void print();           

    };

#endif //COURSE.H

学生班级定义:

Student::Student()
{
    firstName = "None";
    lastName = "None";
    zid = "None";
}


Student::Student(string assignFirst, string assignLast, string assignZID)
{
    firstName = assignFirst;
    lastName = assignLast;
    zid = assignZID;
}



string Student::getLastName()
{
    return lastName;
}


void Student::print()
{
    cout << lastName << ", " << firstName << std::setw(15) << zid << endl; 
}

【问题讨论】:

  • 学生数组是如何定义的?
  • 在调试器中运行你的程序。它会在崩溃发生时停止,并允许您查看(并遍历)调用堆栈。如果崩溃不在您的代码中,则向上调用堆栈直到到达您的代码,然后您可以检查变量的值。或者看看 Valgrind 给你的消息,它告诉你崩溃发生在Student::operator=(Student const&amp;),所以你可以从发布那个函数开始。
  • 可以向我们展示类 Course 声明(头文件)
  • readStudentData 不习惯性地处理从 ifstream 读取。乍一看,您可能正在使用未初始化的数据。正确的成语是while (inFile &gt;&gt; var1 &gt;&gt; var2 &gt;&gt; varN)
  • 您没有对sectionenrolled[section] 进行范围检查。在调试器上检查它们的值。实际上,这个程序足够小,您应该能够单步执行整个函数。在每个语句之后,检查变量的值并将它们与您认为应该的值进行比较。 (它会很快出现。你不需要走太远。)他们不再教调试了吗?

标签: c++ class oop object segmentation-fault


【解决方案1】:

您在几个地方有未定义的行为。第一个是在Course 构造函数中,您可以在其中写入enrolled 越界。第二种是使用该数组中的 未初始化 值作为另一个数组的索引。

如果要在构造函数中初始化整个数组,要么将每个索引一一初始化,要么使用初始化列表将整个数组初始化为相同的值。

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    相关资源
    最近更新 更多