Problem on Reading/Writing a complex object from/to File

Hello everyone!..I have a problem with I/O as i try to read/write a complex object from/to a file,but it throws me segmentation fault during the execution of the program.No compile errors.More specifically i have a class MainSystem which includes an array of Store Objects(another complex class) and an array of CLientDB objects.Each of the above two classes has a number of primitive-type variables and other objects variables.In my way,i created a function on the MainSystem class which is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void MainSystem::saveStateToDisk()
{
    ofstream out;
    out.open("log.dat",ios::out|ios::binary);
    if(!out)
        cout<<"File not open"<<endl;
    else
    {
        out.write((char*)&store,sizeof(Store));  //Store Object
        out.write((char*)&database,sizeof(ClientDB));   //CleintDb Object
        out.write((char*)&rents,sizeof(struct rentInfo*)*rentSize);  //array of structs
        out.write((char*)&rentSize,sizeof(int));
        out.write((char*)&rented,sizeof(Multimedia*)*multimediaRentedSize);  //Array of pointers to Multimedia objects
        out.write((char*)&multimediaRentedSize,sizeof(int));
        out.close();
    }
}


In the main.cpp i created the loadFromDisk function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
MainSystem* load()
{
    MainSystem* ms;
    ifstream in;
    string line;
    in.open("log.dat",ios::in|ios::binary);
    if(!in)
    {
        cout<<"File not open"<<endl;
        return NULL;
    }
    else
    {
        ms=new MainSystem();
        ClientDB* db=new ClientDB();
        Store* s=new Store();;
        rentInfo** rents;
        int rsize;
        Multimedia** rented;
        int multimediaRentedSize;
        in.read((char*)&db,sizeof(ClientDB));
        ms->setClientDatabase(db);
        in.read((char*)&s,sizeof(Store));
        ms->setProductStore(s);    //HERE IT CRASHES
        in.read((char*)&rsize,sizeof(int));
        ms->setRentsSize(rsize);
        in.read((char*)&rents,sizeof(rentInfo*)*rsize);
        ms->setRents(rents);
        in.read((char*)&multimediaRentedSize,sizeof(int));
        ms->setMultimediaRentedSize(multimediaRentedSize);
        in.read((char*)&rented,sizeof(Multimedia*)*multimediaRentedSize);
        ms->setRented(rented);
        in.close();
        return ms;
    }
}


As i said above,no compile errors.During debugging though i noticed that the program crashes during the setup of the productstore.I would expect it to crash in the previous line during the setup of the ClientDB object but it didn't.Any ideas of how to solve this problem?

IDE:CodeBlocks 10.05
OS:Ubuntu 12.10 64-bit
> I would expect it to crash in the previous line during the setup of the ClientDB object
I suppose that you do know what is wrong with that line

undefined behaviour is undefined.
As a suggestion, don't try to make your code crash.
Yes of course!...But i'm also not sure about the way i handle the I/O proccess.Is the above code a correct handling for this issue?
Topic archived. No new replies allowed.