Zaid Saeed
Zaid Saeed

Reputation: 199

Malloc error in c++?

I am doing a project for class and I believe I have the framework correct. Well besides the fact that it doesn't work. Usually with syntax or logic errors, I can pinpoint a location where I messed up, but this time I am completely lost and I have no idea how to fix this. Here is the what it throws out at me:

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

Yea I have no idea what I did. But I seemed to activate a trap card or something. Anyways you all probably need to see my code so here it is.

//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;

}

And here is the 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;
}

If you need to see my other two files just holla at me. Any help would be great cause I am just stuck on this roadblock.

Thanks!

Upvotes: 0

Views: 158

Answers (2)

Don Larynx
Don Larynx

Reputation: 695

Not only do you never store information in the dynamic array S, but you seem to delete S but it never had any stored data.

Also, you must use delete[] S unless you only want to deallocate the contents of the first memory address.

Upvotes: 1

Ishamael
Ishamael

Reputation: 12795

There are two issues in your code. First, you allocate an array (S = new Student[50]), but deallocate it as it was not an array (delete S instead of delete[] S). If you allocate something with new[], you must delete it with delete[].

Secondly, you delete in a loop. If you allocate something once, you should only delete it once. Move delete S two rows below, right before inFile.close(), and replace it with delete[] S, and it should work just fine.

EDIT: another issue in your code, that is not related to the error you see, is that you allocate 50 elements (= new Student[50]), but access 51 (i < 51). Either allocate 51 element, or change you loop to only make 50 iterations.

Upvotes: 0

Related Questions