Двойное освобождение или коррупция (fasttop): 0x08666178 ***
Я пытаюсь реализовать хеширование (сцепление) в файле в C++, используя класс Fstream. Все работает нормально, записи хешируются и добавляются в соответствующие позиции в файле, но когда я пытаюсь добавить синоним записи к существующим данным в этом месте, программа безоговорочно прерывается.
Ошибка в `./a.out': двойное освобождение или повреждение (fasttop): 0x0994e178 Прервано (дамп памяти)
Я искал весь stackru и пытался преодолеть это, но безрезультатно. Может ли кто-нибудь, пожалуйста, указать на ошибку и заставить ее работать. Заранее спасибо. Вот код:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#define table_size 10
#define h(x,i) (x+i)%table_size
using namespace std;
class record{
public:
int rollno, marks, chain;
string name;
record()
{
rollno = marks = chain = -1;
name = "";
}
void display();
};
void insert();
void show_record();
void disp_header();
int traverse( fstream& fp, int loc );
int append( fstream& fp, int rollno, int inc );
int main()
{
record a;
//creating memory space in file record.bin
ofstream fp("record.dat", ios::binary);
for( int i = 0 ; i < table_size ; i++ )
{
fp.write( (char*)&a, sizeof(a) );
}
fp.close();
//ends
int ch;
do{
cout << "\n_____FILE-HASHING_____\n1.Insert\n2.Display\n3.Exit\n--->";
cin >> ch;
switch(ch)
{
case 1://inserting
insert();
break;
case 2://displaying
disp_header();
show_record();
break;
}
}while( ch != 3 );
return 0;
}
void disp_header()
{
cout << setfill('-') << setw(1) << "+" << setw(5) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+"
<< setw(5) << "-" << setw(1) << "+" << setw(5) << "-" << setw(1) << "+" << endl;
cout << setfill(' ') << setw(1) << "|" << setw(5) << "ROLL" << setw(1) << "|" << setw(15) << "NAME" << setw(1) << "|"
<< setw(5) << "MARKS" << setw(1) << "|" << setw(5) << "CHAIN" << setw(1) << "|" << endl;
}
void record::display()
{
cout << setfill('-') << setw(1) << "+" << setw(5) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+"
<< setw(5) << "-" << setw(1) << "+" << setw(5) << "-" << setw(1) << "+" << endl;
cout << setfill(' ') << setw(1) << "|" << setw(5) << rollno << setw(1) << "|" << setw(15) << name << setw(1) << "|"
<< setw(5) << marks << setw(1) << "|" << setw(5) << chain << setw(1) << "|" << endl;
}
int traverse( fstream& fp, int loc )
{
int flag = 1;
record temp;
while( flag )
{
fp.seekg( loc*sizeof(temp), ios::beg );
fp.read( (char*)&temp, sizeof(temp) );
if( temp.chain == -1 )
{
flag = 0;
}
else
{
loc = temp.chain;
}
}
return loc;
}
int append( fstream& fp, int rollno, int inc )
{
int flag = 1, loc;
record in;
while(flag)
{
loc = h(rollno, inc);
fp.seekg( loc*sizeof(in), ios::beg );
fp.read( (char*)&in, sizeof(in) );
if( in.rollno == -1 || h(in.rollno, 0) == loc )
{
flag = 0;
}
else
{
++inc;
}
}
return loc;
}
void insert()
{
fstream fp("record.dat", ios::in | ios::out | ios::binary);
record in, out;
int loc, end, inc = 0;
cout << "\nRollno: ";
cin >> in.rollno;
cout << "Name: ";
cin >> in.name;
cout << "Marks: ";
cin >> in.marks;
loc = h(in.rollno, 0);
fp.seekg( loc*sizeof(out), ios::beg );
fp.read( (char*)&out, sizeof(out) );
if( out.rollno == -1 )
{
fp.seekp( loc*sizeof(in), ios::beg );
fp.write( (char*)&in, sizeof(in) );
}
else if( h(out.rollno, 0) == loc )
{
end = traverse( fp, loc );
inc = end;
loc = append( fp, in.rollno, abs(h(in.rollno, 0)-end) + 1 );
fp.seekg( loc*sizeof(in), ios::beg );
fp.write( (char*)&in, sizeof(in) );
fp.seekg( end*sizeof(in), ios::beg );
fp.read( (char*)&out, sizeof(out) );
out.chain = end;
fp.seekg( end*sizeof(in), ios::beg );
fp.write( (char*)&in, sizeof(in) );
}
fp.close();
}
void show_record()
{
fstream fp("record.dat");
record out;
while( fp.read( (char*)&out, sizeof(out) ) )
{
out.display();
}
fp.close();
}