2010-02-24 3 views
2

할당되지 않은 해제하고 다음과 같은 오류와 함께 실패되고 : 파일 input.txt을의C++ 과부하 >> 나는 다음과 같은 코드를 시도하고

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

있습니다 내용 : 그것은 전체가 권한 및 파일이 디버거에서 성공적으로 열립니다. 도와주세요.

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; 
    }  
} 
+2

이 문제인가? : http://stackoverflow.com/questions/2234557/c-using-getline-prints-pointer-being-freed-was-not-allocated-in-xcode – ergosys

답변

0

코드는 바로 내 모든 보인다 - (? 아마도 설치 문제) 나는 당신이 정확히 크지 않은 일부 조각을 할 문제가 다른 곳에서 말하고 싶지만, 아무것도 원인이없는 것을 큰 문제 (예 : DataType::getKey은 사용되지 않습니다. operator<(DataType, DataType)은 사용되지 않습니다. operator<<은 값 대신 const 참조를 취해야합니다.)

+0

Mac에서 xcode를 사용하고 있습니다. 비슷한 문제가보고되었습니다. 그러나 아이폰 개발자는 게시물에서 유용한 정보를 얻을 수 없습니다. 64 비트 스노우 레오파인지 확실하지 않습니다. 문제가 발생하거나 내가 누락 된 어떤 간단한 원인 – Kiran

1

분명히 코드에 문제가있는 것을 볼 수 없습니다. 불필요한 복사가 진행되고 있습니다 (다양한 연산자는 객체가 복사되는 것을 방지하기 위해 객체가 아닌 (실제로는 바람직하게는 const DataType &)가되어야합니다. 그렇지 않으면 stdio.h가 포함되지 않도록 제거합니다. 코드에 대한 당신이 여기에 표시하고 있다는해야합니다.의

없음 이상하지만, 당신이보고있는 오류를 트리거 없습니다. 당신이 우리를 표시하지 않는 다른 코드가 있습니까?

관련 문제