C++ 为什么不是';这个程序不会崩溃吗?(浅版)

C++ 为什么不是';这个程序不会崩溃吗?(浅版),c++,debugging,visual-studio-code,gdb,shallow-copy,C++,Debugging,Visual Studio Code,Gdb,Shallow Copy,我正在学习复制构造函数和浅层和深层复制。我在看这个视频: 下面的代码直接从视频中复制,并演示浅复制。。。(视频中的9:30) 此代码在运行后会崩溃,因为解构器将尝试两次释放相同的内存(首先用于string,然后用于string2)。第一次删除应该可以正常工作,但是第二次删除应该会导致程序崩溃,因为我们正在尝试删除未分配的内存 令人惊讶的是,这在我的情况下没有发生。我在命令提示符下使用g++copying\u和

我正在学习复制构造函数和浅层和深层复制。我在看这个视频:

下面的代码直接从视频中复制,并演示浅复制。。。(视频中的9:30)

此代码在运行后会崩溃,因为解构器将尝试两次释放相同的内存(首先用于
string
,然后用于
string2
)。第一次删除应该可以正常工作,但是第二次删除应该会导致程序崩溃,因为我们正在尝试删除未分配的内存

令人惊讶的是,这在我的情况下没有发生。我在命令提示符下使用
g++copying\u和a.exe运行它。没有错误

#include<iostream>
#include<cstring>
#include<string>

using std::endl;
using std::cout;


class String
{
private:
    char* m_Buffer;
    unsigned int m_size;
public:
    String(const char* string)
    {
        m_size = strlen(string);
        m_Buffer = new char[m_size+1];
        memcpy(m_Buffer,string,m_size);
        m_Buffer[m_size] = 0;
    }

    ~String()
    {
        delete [] m_Buffer;
    }


    friend std::ostream& operator << (std::ostream& stream, const String& string);
};


std::ostream& operator<<(std::ostream& stream, const String& string)
{
    stream<< string.m_Buffer;
    return stream;
}


int main()
{
    String string = "My string";
    String string2 = string;

    cout<<string2<<endl;
    cout<<string;
    return 0;
}
这一行混淆了我的字符串[次1(进程44892)正常退出]
。该代码如何正常退出

视频中的人正在使用VS代码。。。如何在命令提示符中获得相同的错误

崩溃,如视频中所示:

(我现在还没有vs代码(可能以后再安装))

对于我(Fedora 32 x86_64),它确实会崩溃:

$ g++ -o q q.C -Wall -g;./q
My string
free(): double free detected in tcache 2
Aborted
但我绝对同意这样的代码看起来运行良好。这就是为什么有:

如果您不想要或甚至无法使用
-fsanize=address
重新编译程序,您可以使用(但速度较慢,无法检测所有内容):


未定义的行为可能导致任何结果,包括明显的良好功能。保证不会撞车哦。。。我认为我使用gdb是错误的或者类似的。调试这类错误一定是一场噩梦:(@Yatin)这就是为什么你需要一个好的调试器。一个好的调试器比一个好的编译器要好。
$ g++ -o q q.C -Wall -g;./q
My string
free(): double free detected in tcache 2
Aborted
$ g++ -o q q.C -Wall -g -fsanitize=address;./q
My string
=================================================================
==3388436==ERROR: AddressSanitizer: attempting double-free on 0x602000000010 in thread T0:
    #0 0x7fb76f262cd7 in operator delete[](void*) (/lib64/libasan.so.6+0xb2cd7)
    #1 0x4016e9 in String::~String() /home/jkratoch/t/q.C:25
    #2 0x40144b in main /home/jkratoch/t/q.C:42
    #3 0x7fb76ecbc041 in __libc_start_main ../csu/libc-start.c:308
    #4 0x40120d in _start (/quad/home/jkratoch/t/q+0x40120d)

0x602000000010 is located 0 bytes inside of 10-byte region [0x602000000010,0x60200000001a)
freed by thread T0 here:
    #0 0x7fb76f262cd7 in operator delete[](void*) (/lib64/libasan.so.6+0xb2cd7)
    #1 0x4016e9 in String::~String() /home/jkratoch/t/q.C:25
    #2 0x40143e in main /home/jkratoch/t/q.C:43
    #3 0x7fb76ecbc041 in __libc_start_main ../csu/libc-start.c:308

previously allocated by thread T0 here:
    #0 0x7fb76f2621d7 in operator new[](unsigned long) (/lib64/libasan.so.6+0xb21d7)
    #1 0x4015d6 in String::String(char const*) /home/jkratoch/t/q.C:18
    #2 0x4013a0 in main /home/jkratoch/t/q.C:42
    #3 0x7fb76ecbc041 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: double-free (/lib64/libasan.so.6+0xb2cd7) in operator delete[](void*)
==3388436==ABORTING
$ g++ -o q q.C -Wall -g;valgrind ./q
==3388447== Memcheck, a memory error detector
==3388447== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3388447== Using Valgrind-3.16.0 and LibVEX; rerun with -h for copyright info
==3388447== Command: ./q
==3388447== 
My string
==3388447== Invalid free() / delete / delete[] / realloc()
==3388447==    at 0x483C59C: operator delete[](void*) (vg_replace_malloc.c:649)
==3388447==    by 0x40139E: String::~String() (q.C:25)
==3388447==    by 0x401277: main (q.C:42)
==3388447==  Address 0x4db5c80 is 0 bytes inside a block of size 10 free'd
==3388447==    at 0x483C59C: operator delete[](void*) (vg_replace_malloc.c:649)
==3388447==    by 0x40139E: String::~String() (q.C:25)
==3388447==    by 0x40126B: main (q.C:43)
==3388447==  Block was alloc'd at
==3388447==    at 0x483B582: operator new[](unsigned long) (vg_replace_malloc.c:431)
==3388447==    by 0x401334: String::String(char const*) (q.C:18)
==3388447==    by 0x40121B: main (q.C:42)
==3388447== 
My string==3388447== 
==3388447== HEAP SUMMARY:
==3388447==     in use at exit: 0 bytes in 0 blocks
==3388447==   total heap usage: 3 allocs, 4 frees, 73,738 bytes allocated
==3388447== 
==3388447== All heap blocks were freed -- no leaks are possible
==3388447== 
==3388447== For lists of detected and suppressed errors, rerun with: -s
==3388447== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)