C ++中的Malloc错误?

扎伊德·赛义德(Zaid Saeed)

我正在为一个班级做一个项目,我相信我的框架是正确的。好吧,除了它不起作用的事实。通常由于语法或逻辑错误,我可以指出我搞砸的位置,但是这次我完全迷路了,不知道如何解决。这是它扔给我的东西:

DELETE
DELETE
heapOfStudents(642,0x7fff74944300) malloc: *** error for object 0x7fd2d2803208: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

是的,我不知道我做了什么。但是我似乎激活了陷阱卡之类的东西。无论如何,你们所有人都可能需要查看我的代码,所以就在这里。

//main.cpp
#include <string>
#include <iostream>
#include <fstream>
#include "student.h"
#include "address.h"
#include "date.h"

using namespace std;

int main(){

    Student *S = new Student[50];

    ifstream inFile;
    inFile.open("data.dat");

    string lines[51];
    string item;
    int x = 0;
    while(!inFile.eof()){
        getline(inFile, item);
        if(item != ""){
            lines[x] = item;
            x++;
        }
    }

    for (int i = 0; i < 51; i++){
        string line = lines[i];
        string delimiter = ",";

        size_t pos = 0;
        string sub;
        x = 0;

        while((pos = line.find(delimiter)) != string::npos){
            sub = line.substr(0, pos);
            line.erase(0, pos + delimiter.length());
            if(x == 0){
                S[i].setLName(sub);
            }else if(x == 1){
                S[i].setFName(sub);
            }else if(x == 2 || x == 3 || x == 4 || x == 5 || x == 6){
                S[i].setAddressInfo(sub, x - 2);
            }else if(x == 7 || x == 8){
                S[i].setDate(sub, x - 7);
            }else if(x == 9){
                S[i].setGPA(sub);
            }else{
                S[i].setCredHours(line);
            }
            x++;

            delete S;
        }

    }

    inFile.close();

    return 0;

}

这是Student.cpp

//student.cpp
#include <string>
#include <iostream>
#include "student.h"

using namespace std;

Student::Student(){

}

void Student::setFName(string fName){
    Student::fName = fName;
}

void Student::setLName(string lName){
    Student::lName = lName;
}

void Student::setGPA(string GPA){
    Student::GPA = GPA;
}

void Student::setCredHours(string credHours){
    Student::credHours = credHours;
}

void Student::setAddressInfo(string info, int part){
    if(part == 0){
        Student::home.setAddress1(info);
    }else if(part == 1){
        Student::home.setAddress2(info);
    }else if(part == 2){
        Student::home.setCity(info);
    }else if(part == 3){
        Student::home.setState(info);
    }else if(part == 4){
        Student::home.setZip(info);
    }
}

void Student::setDate(string info, int part){
    if(part == 0){
        Student::dateOfBirth.setDate(info);
    }else if(part == 2){
        Student::dateOfComp.setDate(info);
    }
}

string Student::getFName(){
    return Student::fName;
}

string Student::getLName(){
    return Student::lName;
}

string Student::getGPA(){
    return Student::GPA;
}

string Student::getCredHours(){
    return Student::credHours;
}

void Student::reportStudent(){

}

void Student::unsortedPrint(){

}

Student::~Student(){
    cout << "DELETE" << endl;
}

如果您需要查看我的其他两个文件,请holla来找我。任何帮助都是很好的原因,因为我只是被困在这个障碍中。

谢谢!

唐·拉恩克斯

您不仅不会将信息存储在动态数组中S,而且您似乎delete S也从未存储任何数据。

另外,delete[] S除非您只想取消分配第一个内存地址的内容,否则必须使用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章