LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2020, 02:28 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
double free or corruption (fasttop)


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.

Last edited by rahulvishwakarma; 01-28-2020 at 02:30 AM.
 
Old 01-28-2020, 04:47 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You cannot write a 'string' into a file this way. The method you commented out would work.
Code:
struct person
{
    char name[50];
    int age;
    double height;
}
Additional reading: https://isocpp.org/wiki/faq/serialization

Last edited by NevemTeve; 01-28-2020 at 05:49 AM.
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT : *** glibc detected *** : double free or corruption (fasttop) kuldippa Red Hat 4 10-11-2009 09:02 PM
FC3 glibc detected *** double free or corruption: josedragone Fedora 5 09-17-2009 10:16 PM
*** glibc detected *** double free or corruption (!prev): 0x082c1120 *** eXor Slackware 6 04-11-2008 08:47 AM
*** glibc detected *** double free or corruption (fasttop): 0x080b0a38 *** Accrio Linux - General 2 03-28-2007 01:22 AM
Open Office user install error: *** glibc detected *** double free or corruption: 0xb r_jensen11 Linux - General 6 01-16-2005 06:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration