Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取/写入文件时双重释放或损坏_C++_File Io - Fatal编程技术网

C++ 读取/写入文件时双重释放或损坏

C++ 读取/写入文件时双重释放或损坏,c++,file-io,C++,File Io,这是我的密码: #include <iostream> #include <string> #include <fstream> using namespace std; class student{ private: string nm, addr; int roll; public: student(string name="a", string address="a", int rollnumber=0): nm(na

这是我的密码:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class student{
private:
    string nm, addr;
    int roll;
public:
    student(string name="a", string address="a", int rollnumber=0):
    nm(name), addr(address), roll(rollnumber){}
    void show(){
        cout<<nm<<endl<<addr<<endl<<roll<<endl;
    }
};

int main(){
    fstream file1;
    file1.open("obj.file", ios::in|ios::out|ios::binary|ios::ate);
    student s1("cipher", "MyAddress", 21);
    student s2;
    file1.write(reinterpret_cast<char *>(&s1), sizeof(s1));
    file1.flush();
    file1.seekg(0,ios::beg);
    file1.read(reinterpret_cast<char *>(&s2), sizeof(s2));
    s2.show();
    return 0;
}

您正在逐字节将
s1
复制到
s2
(及其关联的数据成员)。这适用于类型,但不能保证适用于复杂类型。不能以这种方式复制字符串。当它们的析构函数运行时,它们很可能会尝试释放同一块内存(它们的内部数据存储),即双
delete

为了避免这种情况,您绕过了string类所具有的复制机制。这和调用
memcpy
或类似的东西没什么区别。不行。您无法像这样持久化非POD类型


即使你没有犯错误,从逻辑上讲,它仍然是错误的。想想看;这些字符串将动态分配其内部内存存储,即,它们将存储指向某个内存块的指针。一旦字符串的析构函数运行,该指针将无效,如果一周后您决定反序列化该文件,则该指针肯定无效。

当您写入s1的内容时,实际上是在字符串内复制指针。然后将其写入s2的字符串中。当s1和s2调用解构器时,它们都将释放相同的字符串指针,这是您的双空闲

$ ./fh2 
cipher
MyAddress
21
*** Error in `./fh2': double free or corruption (fasttop): 0x08e14178 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767c2)[0xb756f7c2]
/lib/i386-linux-gnu/libc.so.6(+0x77510)[0xb7570510]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb7713a3f]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1b)[0xb777845b]
/usr/lib/i386-linux-gnu/libstdc++.so.6(+0x4671a)[0xb771171a]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSsD1Ev+0x2e)[0xb77784be]
./fh2[0x804902d]
./fh2[0x8048dc0]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb7512905]
./fh2[0x8048a91]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:05 2793609    /home/cipher/cipher-codes/practice-for-cpp-exam/fh2
0804a000-0804b000 r--p 00001000 08:05 2793609    /home/cipher/cipher-codes/practice-for-cpp-exam/fh2
0804b000-0804c000 rw-p 00002000 08:05 2793609    /home/cipher/cipher-codes/practice-for-cpp-exam/fh2
08e12000-08e33000 rw-p 00000000 00:00 0          [heap]
b74b4000-b74b6000 rw-p 00000000 00:00 0 
b74b6000-b74f7000 r-xp 00000000 08:05 11707309   /lib/i386-linux-gnu/libm-2.17.so
b74f7000-b74f8000 r--p 00040000 08:05 11707309   /lib/i386-linux-gnu/libm-2.17.so
b74f8000-b74f9000 rw-p 00041000 08:05 11707309   /lib/i386-linux-gnu/libm-2.17.so
b74f9000-b76a7000 r-xp 00000000 08:05 11707258   /lib/i386-linux-gnu/libc-2.17.so
b76a7000-b76a9000 r--p 001ae000 08:05 11707258   /lib/i386-linux-gnu/libc-2.17.so
b76a9000-b76aa000 rw-p 001b0000 08:05 11707258   /lib/i386-linux-gnu/libc-2.17.so
b76aa000-b76ae000 rw-p 00000000 00:00 0 
b76ae000-b76c9000 r-xp 00000000 08:05 11706373   /lib/i386-linux-gnu/libgcc_s.so.1
b76c9000-b76ca000 r--p 0001a000 08:05 11706373   /lib/i386-linux-gnu/libgcc_s.so.1
b76ca000-b76cb000 rw-p 0001b000 08:05 11706373   /lib/i386-linux-gnu/libgcc_s.so.1
b76cb000-b77a8000 r-xp 00000000 08:05 8300861    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77a8000-b77ac000 r--p 000dc000 08:05 8300861    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77ac000-b77ad000 rw-p 000e0000 08:05 8300861    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77ad000-b77b4000 rw-p 00000000 00:00 0 
b77ca000-b77ce000 rw-p 00000000 00:00 0 
b77ce000-b77cf000 r-xp 00000000 00:00 0          [vdso]
b77cf000-b77ef000 r-xp 00000000 08:05 11707234   /lib/i386-linux-gnu/ld-2.17.so
b77ef000-b77f0000 r--p 0001f000 08:05 11707234   /lib/i386-linux-gnu/ld-2.17.so
b77f0000-b77f1000 rw-p 00020000 08:05 11707234   /lib/i386-linux-gnu/ld-2.17.so
bfd7c000-bfd9d000 rw-p 00000000 00:00 0          [stack]
[1]    24855 abort (core dumped)  ./fh2