#include <iostream>
#include <string.h>
using namespace std;
class A
{
public:
char *token = new char[10];
A()
{
strcpy(token, "hello");
}
~A()
{
if(token != NULL)
{
delete[] token;
token = NULL;
}
cout<<"destroyed"<<endl;
}
};
int main() {
A a;
if(1)
{
A b = a;
cout<<b.token<<endl;
}
cout<<a.token<<endl;
cout<<"End";
}
Standard input is empty
hello destroyed End
*** Error in `./prog': double free or corruption (fasttop): 0x000055ae728bbc20 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x2ba6c3b63bcb] /lib/x86_64-linux-gnu/libc.so.6(+0x76f96)[0x2ba6c3b69f96] /lib/x86_64-linux-gnu/libc.so.6(+0x7778e)[0x2ba6c3b6a78e] ./prog(+0xde1)[0x55ae72246de1] ./prog(+0xc2b)[0x55ae72246c2b] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x2ba6c3b132b1] ./prog(+0xcba)[0x55ae72246cba] ======= Memory map: ======== 2ba6c2e13000-2ba6c2e36000 r-xp 00000000 fd:00 2840974 /lib/x86_64-linux-gnu/ld-2.24.so 2ba6c2e36000-2ba6c2e3a000 rw-p 00000000 00:00 0 2ba6c2e43000-2ba6c2e48000 rw-p 00000000 00:00 0 2ba6c3036000-2ba6c3037000 r--p 00023000 fd:00 2840974 /lib/x86_64-linux-gnu/ld-2.24.so 2ba6c3037000-2ba6c3038000 rw-p 00024000 fd:00 2840974 /lib/x86_64-linux-gnu/ld-2.24.so 2ba6c3038000-2ba6c3039000 rw-p 00000000 00:00 0 2ba6c3039000-2ba6c31ab000 r-xp 00000000 fd:00 2967755 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22 2ba6c31ab000-2ba6c33ab000 ---p 00172000 fd:00 2967755 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22 2ba6c33ab000-2ba6c33b5000 r--p 00172000 fd:00 2967755 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22 2ba6c33b5000-2ba6c33b7000 rw-p 0017c000 fd:00 2967755 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22 2ba6c33b7000-2ba6c33bb000 rw-p 00000000 00:00 0 2ba6c33bb000-2ba6c34be000 r-xp 00000000 fd:00 2841003 /lib/x86_64-linux-gnu/libm-2.24.so 2ba6c34be000-2ba6c36bd000 ---p 00103000 fd:00 2841003 /lib/x86_64-linux-gnu/libm-2.24.so 2ba6c36bd000-2ba6c36be000 r--p 00102000 fd:00 2841003 /lib/x86_64-linux-gnu/libm-2.24.so 2ba6c36be000-2ba6c36bf000 rw-p 00103000 fd:00 2841003 /lib/x86_64-linux-gnu/libm-2.24.so 2ba6c36bf000-2ba6c36d5000 r-xp 00000000 fd:00 2840941 /lib/x86_64-linux-gnu/libgcc_s.so.1 2ba6c36d5000-2ba6c38d4000 ---p 00016000 fd:00 2840941 /lib/x86_64-linux-gnu/libgcc_s.so.1 2ba6c38d4000-2ba6c38d5000 r--p 00015000 fd:00 2840941 /lib/x86_64-linux-gnu/libgcc_s.so.1 2ba6c38d5000-2ba6c38d6000 rw-p 00016000 fd:00 2840941 /lib/x86_64-linux-gnu/libgcc_s.so.1 2ba6c38d6000-2ba6c38ee000 r-xp 00000000 fd:00 2840960 /lib/x86_64-linux-gnu/libpthread-2.24.so 2ba6c38ee000-2ba6c3aed000 ---p 00018000 fd:00 2840960 /lib/x86_64-linux-gnu/libpthread-2.24.so 2ba6c3aed000-2ba6c3aee000 r--p 00017000 fd:00 2840960 /lib/x86_64-linux-gnu/libpthread-2.24.so 2ba6c3aee000-2ba6c3aef000 rw-p 00018000 fd:00 2840960 /lib/x86_64-linux-gnu/libpthread-2.24.so 2ba6c3aef000-2ba6c3af3000 rw-p 00000000 00:00 0 2ba6c3af3000-2ba6c3c88000 r-xp 00000000 fd:00 2841097 /lib/x86_64-linux-gnu/libc-2.24.so 2ba6c3c88000-2ba6c3e87000 ---p 00195000 fd:00 2841097 /lib/x86_64-linux-gnu/libc-2.24.so 2ba6c3e87000-2ba6c3e8b000 r--p 00194000 fd:00 2841097 /lib/x86_64-linux-gnu/libc-2.24.so 2ba6c3e8b000-2ba6c3e8d000 rw-p 00198000 fd:00 2841097 /lib/x86_64-linux-gnu/libc-2.24.so 2ba6c3e8d000-2ba6c3e91000 rw-p 00000000 00:00 0 2ba6c4000000-2ba6c4021000 rw-p 00000000 00:00 0 2ba6c4021000-2ba6c8000000 ---p 00000000 00:00 0 55ae72246000-55ae72248000 r-xp 00000000 fd:00 23353346 /home/J5CvcL/prog 55ae72447000-55ae72448000 r--p 00001000 fd:00 23353346 /home/J5CvcL/prog 55ae72448000-55ae72449000 rw-p 00002000 fd:00 23353346 /home/J5CvcL/prog 55ae728aa000-55ae728dc000 rw-p 00000000 00:00 0 [heap] 7fff3f925000-7fff3f946000 rw-p 00000000 00:00 0 [stack] 7fff3f990000-7fff3f992000 r-xp 00000000 00:00 0 [vdso] 7fff3f992000-7fff3f994000 r--p 00000000 00:00 0 [vvar] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]