#Exception thrown: read access violation.**this** was 0xFFFFFFFFFFFFFFDF.

22 messages · Page 1 of 1 (latest)

jolly anvil
#

I keep getting this error in my print statement

void Student::print() {
    cout << getStudentID() << "\t";
    cout << "First Name: " << getFirstName() << "\t";
    cout << "Last Name: " << getLastName() << "\t";
    cout << "Email Address: " << getEmailAddress() << "\t";
    cout << "Age:\t" << age << "\t";
    cout << "DaysInCourse: {" << getDaysToCompleteEachCourse()[0] << ", " << getDaysToCompleteEachCourse()[1] <<
        ", " << getDaysToCompleteEachCourse()[2] << "} ";
    cout << "Degree Program: " << convertEnumToString(getDegreeProgram()) << endl;

I stepped through on the debugger and it seems to happen from any getter method accessed in this function

string Student::getStudentID() {
    return this->studentID;
}
normal spireBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

marsh oasis
#

gonna need to see more code

jolly anvil
#

Should I just paste all the files?

marsh oasis
#

just paste the relevant code from the stack trace

#

toss a breakpoint where it's crashing

#

also worth asking: is the Student dynamically allocated, stack allocated, etc. Have you ensured the object that you're calling print on is valid, etc

jolly anvil
#
main.cpp
int main()
{
    const string studentData[] =

    { "A1,John,Smith,John1989@gm ail.com,20,30,35,40,SECURITY",
        "A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK",
        "A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE",
        "A4,Erin,Black,Erin.black@comcast.net,22,50,58,40,SECURITY",
        "A5,Sample,Sample,sampleemail@gmail.com,20,10,15,20,SOFTWARE" };


    Roster classRoster;
    for (int a = 0; a < 5; a++) {
        string data = studentData[a];
        int start = 0;
        int end = data.find(',');

        string studentInfo[9];
        for (int i = 0; i < 9; ++i) {
            studentInfo[i] = data.substr(start, end - start);
            start = end + 1;
            end = data.find(',', start);
        }

        string studentID = studentInfo[0];
        string firstName = studentInfo[1];
        string lastName = studentInfo[2];
        string emailAddress = studentInfo[3];
        int age = stoi(studentInfo[4]);
        int daysInCourse1 = stoi(studentInfo[5]);
        int daysInCourse2 = stoi(studentInfo[6]);
        int daysInCourse3 = stoi(studentInfo[7]);
        DegreeProgram degreeProgram;

        string degreeProgramStr = studentInfo[8];
        if (degreeProgramStr == "SECURITY") {
            degreeProgram = SECURITY;
        }
        else if (degreeProgramStr == "NETWORK") {
            degreeProgram = NETWORK;
        }
        else if (degreeProgramStr == "SOFTWARE") {
            degreeProgram = SOFTWARE;
        }
        classRoster.add(studentID, firstName, lastName, emailAddress, age, daysInCourse1, daysInCourse2, daysInCourse3, degreeProgram);
    }
    
    classRoster.printAll();
#

so this is main

#
void Roster::add(string studentID, string firstName, string lastName,
    string emailAddress, int age, int daysInCourse1,
    int daysInCourse2, int daysInCourse3, DegreeProgram degreeProgram) {

    int daysForEachCourse[3];
    daysForEachCourse[0] = daysInCourse1;
    daysForEachCourse[1] = daysInCourse2;
    daysForEachCourse[2] = daysInCourse3;
    Student* student = new Student(studentID, firstName, lastName,
        emailAddress, age, daysForEachCourse, degreeProgram);
    for (int i = 0; i < 5; i++) {
        if (classRosterArray[i] == nullptr) {
            classRosterArray[i] = student;
            break;
        }
    }
}
#

here is my add method

#

classRosterArray is an array of pointers

#

Student * classRosterArray[];

#
void Roster::printAll() {
    for (int i = 0; i < 5; i++) {
        if (classRosterArray[i] != nullptr) {
            cout << classRosterArray[i];
            classRosterArray[i]->print();
        }
    }
}

In my print all here, I tried to print the address of the student pointer and it's CCCCCCCCCCCCCCCC

#

But when I print the pointers in my add method, they all seem to be good

#

I feel like im not adding the student pointer to the array correctly

bright wren
#
for (int i = 0; i < 5; i++) {
        if (classRosterArray[i] == nullptr) {
            classRosterArray[i] = student;
            break;
        }
    }

more precisely, this line

if (classRosterArray[i] == nullptr)

will always fail, becouse of the fact that the array is filled with CCCCC+..., make sure to zero out everything in the constructor, or dont use debug mode

#

also the potential reason for the crash might be the same

if (classRosterArray[i] != nullptr) {
            cout << classRosterArray[i];
            classRosterArray[i]->print();
        }

when trying to access the pointers

if (classRosterArray[i] != nullptr)

it will pass the if becouse instead of being nullptr(default), it will be 0xCCCCCCCCCCC+..., which is the default null that MSVC uses in debug mode

jolly anvil
#

That fixed it

#

thank you so much

normal spireBOT
#

@jolly anvil Has your question been resolved? If so, type !solved :)

jolly anvil
#

!solved