tags:

views:

234

answers:

2

Hello friends, I am trying the following code and it fails with the following error:

malloc: *** error for object 0x10000d8c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal:  “SIGABRT”.

Here are contents of file input.txt : It has full permissions and file is successfully opened in debugger. Please help.

Jacob Anderson
Michael Thomson
Joshua Smith
Mathew Matheis
Ethan Evans 
Emily Drake
Emma Patterson
Madison McPhee
Hannah Briens
Ashley Schmidt

.

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <list>
#include <fstream>
#include <string>

#include <stdio.h>

using namespace std;

struct DataType  {
    string lastname;              // (Key) Student's Last Name
    string firstname;     // Student's First Name

    string getKey () const
    { return lastname; }   // Returns the key field
};

ostream& operator << (ostream& os, DataType myData) {
    os<<myData.firstname<< " "<<myData.lastname;
    return os;
}

bool operator < (DataType lhs, DataType rhs) {
    if (lhs.firstname < rhs.firstname)
        return true;
    return false;
}

int main() {
 ifstream studentFile ("input.txt");  // Student file
    list <DataType> students;            // Students
    DataType currStudent;              // One Student (has firstname,lastname)

    if (! studentFile.is_open())
    {
        return -1;
    }
    while (studentFile >> currStudent.firstname >> currStudent.lastname) {
        students.push_back(currStudent);
    }

    list<DataType>::iterator i = students.begin();
    while (i != students.end()) {
        cout << *i << endl ;
        ++i;
    }    
}
A: 

The code looks all right to me -- I'd say the problem is from elsewhere (possibly an installation problem?) You do have some pieces that aren't exactly great, but nothing that should cause a major problem (e.g. DataType::getKey is never used, operator<(DataType, DataType) is never used, operator<< should probably take a const reference instead of a value).

Jerry Coffin
I am using xcode on Mac. I saw similar problem reported by iPhone developers. However, couldn't derive any useful info from the posts. Not sure if it is the 64bit snow leopard causing the issue or something simple that I am missing
Kiran
+1  A: 

I can't see anything obviously wrong with the code. There's some unnecessary copying going on (the various operators should take DataType & (actually, preferably const DataType &) rather than objects as they do now to prevent the objects from being copied. I'd also remove the inclusion of stdio.h as you don't need that for the code you're showing here.

None of the above should trigger the error you're seeing, though. Is there any other code you're not showing us?

Timo Geusch