-4
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class telephone
{
   string name;
   long number;
   public :
     void getdata();
     void display();
};
void telephone :: getdata()
{
  cout<<"Enter the name : ";
  getline(cin,name);
  cout<<"Enter the number : ";
  cin>>number;
}
void telephone :: display()
{
  cout<<"1. Name   : "<<name<<endl;
  cout<<"2. Number : "<<number<<endl;
}
int main()
{
  fstream f;
  telephone p,q;
  f.open("dir.txt",ios::out);
  p.getdata();
  f.write((char*)&p,sizeof(telephone));
  f.close();
  f.open("dir.txt",ios::in);
  while( f.read((char*)&q,sizeof(telephone)))
  {
     q.display();
  }
  f.close(); 
  return 0;
}

I have written this code to write and read data from file in class object.It displays the output but shows some error.

OUTPUT :

Enter the name : rahul
Enter the number : 234546
1. Name   : rahul
2. Number : 234546
*** Error in `./a.out': double free or corruption (fasttop): 0x08f861a8 ***
Aborted (core dumped)

I have tried by using file extension like .txt,.bin,.dat but it showed the same error.Please help me to remove this error.

Community
  • 1
  • 1
Nihal Karne
  • 59
  • 1
  • 6
  • 2
    If you're using C++, use `ifstream`, `ofstream`, and the `<<` and `>>` operators. – AndyG Sep 22 '16 at 20:42
  • 2
    you need serialization because having a string as an object – Raindrop7 Sep 22 '16 at 20:43
  • 2
    Possible duplicate of [C++ Read and write multiple objects of same class](http://stackoverflow.com/questions/18186701/c-read-and-write-multiple-objects-of-same-class) – Raindrop7 Sep 22 '16 at 20:44

1 Answers1

1

Writing a telephone to a file as a binary blob will not work. telephone contains name and name is a std::string. A std::string does not typically contain the string data it represents; it contains a pointer to the string data.

So when you

f.write((char*)&p,sizeof(telephone));

what you actually wrote to the file was not the string data, but a pointer to the string data. This means that

 f.read((char*)&q,sizeof(telephone));

reads back p's pointer to q and that means that p and q both point at the same string data. This is bad.

When p or q go out of scope and are destroyed, they destroy name, and name, like a good little std::string, and frees the memory it points at. This leaves the other object containing a std::string pointing at memory that has been freed, and sooner or later that other object will either be used and invoke undefined behaviour or be destroyed and attempt to free the previously freed memory. This is what is meant in the error message by "double free". The same memory has been freed twice.

In your case, if q is deleted before p, q releases the memory that both p and q point at leaving p pointing at an invalid memory location. A few nanoseconds later p is deleted and p cannot free the already freed memory.

To get around this you must ensure the contents of the std::string are written to the file and then read back. This is called serialization.

Typical solutions to this problem are to write the data to the file in a text format with << and read it back with >> (which may require you to implement the << and >> operators for your class)

class telephone
{
   string name;
   long number;
   public :
     void getdata();
     void display();
     friend std::istream & operator<<(std::istream & out, const telephone & tp);
     friend std::ostream & operator>>(std::ostream & in, telephone & tp);
};

or add serialization functions to the class

class telephone
{
   string name;
   long number;
   public :
     void getdata();
     void display();
     bool serialize(std::iostream & out);
     bool deserialize(std::iostream & in);
};

The writing of these functions is probably the point of this assignment, so I'll stop here. Don't worry. Both are approaches are exceedingly well documented online if you have trouble. I recommend starting with the first option. It is much easier to debug because you can read the text file to see if you got it wrong.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thanks. But how to write definition of overloaded << and >> operator function. – Nihal Karne Sep 26 '16 at 17:29
  • Basic skeletons and ideology are covered here: http://stackoverflow.com/questions/4421706/operator-overloading . The guts of the functions I won't cover, but are basically a bunch of stuff like `in >> phone.name` – user4581301 Sep 26 '16 at 19:37