0

I have a char pointer as a private member of a class. I need to read record from a file and insert it into class array. First, I need to get number of record first then create a myStudent array during runtime. Then insert all the record in. But when I tried to initialize the name field using set method, it gave me Practise1(3278,0x7fff7287e300) malloc:error for object 0x100105578: incorrect checksum for freed object - object was probably modified after being freed. set a breakpoint in malloc_error_break to debug error

if i use debugger to run the program step-by-step, it works perfectly fine with no error, however if i run it normally, it gave me the above error. (Sometimes it works, sometime it doesn't)

Here is a small portion of my code:

myClass.h:

class myClass{
private:
char *name;
public:
  myClass();
  void setName(string);
}

myClass.cpp

myClass:: myClass(){}
void myClass::setName(string x){
  name = new char[x.length()+1];    //my xcode give me signal SIGBART here
  strcpy(name, x.c_str());
}

main.cpp

int main(){
myClass *classArr;
int amountRecord = getRecord(); //check the number of record and return it(assuming it return 5)
classArr = new myClass[amountRecord];

  loadClassData("test.dat",classArr);

  }

void loadClassData(string filename,myClass *classArr){
ifstream ins(filename,ios::in);
int counter = 0;
string className;
string temp;
if(ins.good()){
    while(!ins.eof()){
        className = "";                     
            getline(ins, className,'\n');
            classArr[counter].setName(className);        
        counter++;
}
ins.close();
}
Paul Ang
  • 65
  • 1
  • 1
  • 11

3 Answers3

1

The problem is in how you loop when reading (see Why is “while ( !feof (file) )” always wrong? for why).

This causes the loop to iterate one extra time leading you to use an out-of-bounds index into the classArr array, which leads to undefined behavior and the crash.

Instead do e.g. while (std::getline(ins, className))

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I have an extra function that check for empty line and such,and I used the debugger to check, it loop the same amount as the record. The funny thing is if I use the debugger to print the result, it works perfectly fine. – Paul Ang Aug 22 '15 at 12:07
  • @carrotpie Please try changing the loop, for example to what I show in my updated answer. Check if it works better, because what you do now will simply not work the way you expect. – Some programmer dude Aug 22 '15 at 12:11
  • same error, and it made my program skip reading a line from the .dat file every time it loop – Paul Ang Aug 22 '15 at 12:18
  • 1
    Jaochims right, change the while loop to be like he suggested and then remove the getline function you call before. I guess you left that in, and thats why its skipping lines and giving you the same error. – timdykes Aug 22 '15 at 12:30
0

In function void myClass::setName(string x) you are using some variable called sName.

I have no idea where it's declared, but you should be using the variable x that is passed in the function.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
0

Where sName is come from? I think it should be like this.

myStudent::myStudent(){}
void myStudent::setName(string x){
  name = new char[x.length()+1];    //my xcode give me signal SIGBART here
  strcpy(name, x.c_str());
}
okan
  • 1
  • 2
  • sorry i mistype it in the example, it should be x, not sName – Paul Ang Aug 22 '15 at 11:36
  • you are trying to get integer but you are doing it with char. do it with another way. your problem is setname function. fix it – okan Aug 22 '15 at 11:46