file : binReadAndWrtie.cpp
hi to all, I've centos7.5 in VM . I am trying to buils program
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#pragma pack(push, 1)
struct person
{
string strname;
// char name[50];
int age;
double height;
};
#pragma pack(pop)
int main()
{
person someone = { "rahul", 38, 5.6 };
person sm2;
string filename = "test.bin";
ofstream outFile;
outFile.open(filename, ios::binary);
if(outFile.is_open())
{
outFile.write(reinterpret_cast<char *>(&someone),sizeof(person));
outFile.close();
}
else
{
cout << "cannot create file : " + filename;
}
fstream inFile;
inFile.open(filename, ios::binary|ios::in);
if(inFile.is_open())
{
inFile.read(reinterpret_cast<char *>(&sm2), sizeof(person));
inFile.close();
}
else
{
cout << "cannot read file : " + filename;
}
cout << "sm2 : age = " << sm2.age << " sm2 : strname = " << sm2.strname << " sm2 : height " << sm2.height << endl;
return 0;
}
but having this problem :-
Code:
[rahul@centos7client Debug]$ ./binReadAndWrtie
sm2 : age = 38 sm2 : strname = rahul sm2 : height 5.6
*** Error in `./binReadAndWrtie': double free or corruption (fasttop): 0x00000000015bb010 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81679)[0x7f3cb20aa679]
/lib64/libstdc++.so.6(_ZNSsD1Ev+0x43)[0x7f3cb29ccb63]
./binReadAndWrtie[0x40166e]
./binReadAndWrtie[0x401505]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f3cb204b505]
./binReadAndWrtie[0x4011a9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:02 1214883 /opt/projects/codeblocks/c++/binaryfiles/binReadAndWrtie/bin/Debug/binReadAndWrtie
00601000-00602000 r--p 00001000 08:02 1214883 /opt/projects/codeblocks/c++/binaryfiles/binReadAndWrtie/bin/Debug/binReadAndWrtie
00602000-00603000 rw-p 00002000 08:02 1214883 /opt/projects/codeblocks/c++/binaryfiles/binReadAndWrtie/bin/Debug/binReadAndWrtie
015bb000-015dc000 rw-p 00000000 00:00 0 [heap]
7f3cac000000-7f3cac021000 rw-p 00000000 00:00 0
7f3cac021000-7f3cb0000000 ---p 00000000 00:00 0
7f3cb2029000-7f3cb21ec000 r-xp 00000000 08:02 1971080 /usr/lib64/libc-2.17.so
7f3cb21ec000-7f3cb23ec000 ---p 001c3000 08:02 1971080 /usr/lib64/libc-2.17.so
7f3cb23ec000-7f3cb23f0000 r--p 001c3000 08:02 1971080 /usr/lib64/libc-2.17.so
7f3cb23f0000-7f3cb23f2000 rw-p 001c7000 08:02 1971080 /usr/lib64/libc-2.17.so
7f3cb23f2000-7f3cb23f7000 rw-p 00000000 00:00 0
7f3cb23f7000-7f3cb240c000 r-xp 00000000 08:02 2023738 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f3cb240c000-7f3cb260b000 ---p 00015000 08:02 2023738 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f3cb260b000-7f3cb260c000 r--p 00014000 08:02 2023738 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f3cb260c000-7f3cb260d000 rw-p 00015000 08:02 2023738 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f3cb260d000-7f3cb270e000 r-xp 00000000 08:02 1971088 /usr/lib64/libm-2.17.so
7f3cb270e000-7f3cb290d000 ---p 00101000 08:02 1971088 /usr/lib64/libm-2.17.so
7f3cb290d000-7f3cb290e000 r--p 00100000 08:02 1971088 /usr/lib64/libm-2.17.so
7f3cb290e000-7f3cb290f000 rw-p 00101000 08:02 1971088 /usr/lib64/libm-2.17.so
7f3cb290f000-7f3cb29f8000 r-xp 00000000 08:02 1971372 /usr/lib64/libstdc++.so.6.0.19
7f3cb29f8000-7f3cb2bf7000 ---p 000e9000 08:02 1971372 /usr/lib64/libstdc++.so.6.0.19
7f3cb2bf7000-7f3cb2bff000 r--p 000e8000 08:02 1971372 /usr/lib64/libstdc++.so.6.0.19
7f3cb2bff000-7f3cb2c01000 rw-p 000f0000 08:02 1971372 /usr/lib64/libstdc++.so.6.0.19
7f3cb2c01000-7f3cb2c16000 rw-p 00000000 00:00 0
7f3cb2c16000-7f3cb2c38000 r-xp 00000000 08:02 1970900 /usr/lib64/ld-2.17.so
7f3cb2dfe000-7f3cb2e03000 rw-p 00000000 00:00 0
7f3cb2e34000-7f3cb2e37000 rw-p 00000000 00:00 0
7f3cb2e37000-7f3cb2e38000 r--p 00021000 08:02 1970900 /usr/lib64/ld-2.17.so
7f3cb2e38000-7f3cb2e39000 rw-p 00022000 08:02 1970900 /usr/lib64/ld-2.17.so
7f3cb2e39000-7f3cb2e3a000 rw-p 00000000 00:00 0
7fff32aa4000-7fff32ac6000 rw-p 00000000 00:00 0 [stack]
7fff32b47000-7fff32b49000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
how to solve this.