* Source Code

#include <iostream>
#include <cstring>
using namespace std;

class myClass{

        private:
                int a;
                char *name;

        public:
                myClass(){ }
                myClass(int _a, const char *_name){
                        a = _a;
                        name = new char[strlen(_name)+1];
                        strcpy(name, _name);
                }
#if 0
                myClass(const myClass &T){
                        a = T.a;
                        name = new char[strlen(T.name)+1];
                        strcpy(name, T.name);

                }
#endif
                ~myClass(){
                        delete []name;
                }

                void showData(){
                        cout << a <<endl;
                        cout << name << endl;
                }

};

int main(int argc, char *argv[]){
        myClass A(4,"spider man");
        myClass B(A);

        B.showData();
}

* Compilation

 

g++ -g classbasic.cpp

 

 

* Error

 

[root@localhost PracticeAlgorithm]# ./a.out
4
spider man
*** Error in `./a.out': double free or corruption (fasttop): 0x080dc008 ***
======= Backtrace: =========
/lib/libc.so.6[0x4c4e9b8a]
/lib/libstdc++.so.6(_ZdlPv+0x20)[0x4d2f2940]
/lib/libstdc++.so.6(_ZdaPv+0x1c)[0x4d2f299c]
./a.out[0x80489d6]
./a.out[0x80488ea]
/lib/libc.so.6(__libc_start_main+0xf3)[0x4c48d963]
./a.out[0x80487a1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 fd:02 2762798    /home/broadcom/codes/PracticeAlgorithm/a.out
08049000-0804a000 r--p 00000000 fd:02 2762798    /home/broadcom/codes/PracticeAlgorithm/a.out
0804a000-0804b000 rw-p 00001000 fd:02 2762798    /home/broadcom/codes/PracticeAlgorithm/a.out
080dc000-080fd000 rw-p 00000000 00:00 0          [heap]
4c44c000-4c46b000 r-xp 00000000 fd:01 552980     /usr/lib/ld-2.17.so
4c46b000-4c46c000 r--p 0001e000 fd:01 552980     /usr/lib/ld-2.17.so
4c46c000-4c46d000 rw-p 0001f000 fd:01 552980     /usr/lib/ld-2.17.so
4c474000-4c62c000 r-xp 00000000 fd:01 552982     /usr/lib/libc-2.17.so
4c62c000-4c62e000 r--p 001b7000 fd:01 552982     /usr/lib/libc-2.17.so
4c62e000-4c62f000 rw-p 001b9000 fd:01 552982     /usr/lib/libc-2.17.so
4c62f000-4c632000 rw-p 00000000 00:00 0
4c670000-4c6b1000 r-xp 00000000 fd:01 552996     /usr/lib/libm-2.17.so
4c6b1000-4c6b2000 r--p 00040000 fd:01 552996     /usr/lib/libm-2.17.so
4c6b2000-4c6b3000 rw-p 00041000 fd:01 552996     /usr/lib/libm-2.17.so
4c899000-4c8b4000 r-xp 00000000 fd:01 552999     /usr/lib/libgcc_s-4.8.3-20140911.so.1
4c8b4000-4c8b5000 r--p 0001a000 fd:01 552999     /usr/lib/libgcc_s-4.8.3-20140911.so.1
4c8b5000-4c8b6000 rw-p 0001b000 fd:01 552999     /usr/lib/libgcc_s-4.8.3-20140911.so.1
4d2a7000-4d388000 r-xp 00000000 fd:01 543920     /usr/lib/libstdc++.so.6.0.19
4d388000-4d38c000 r--p 000e1000 fd:01 543920     /usr/lib/libstdc++.so.6.0.19
4d38c000-4d38d000 rw-p 000e5000 fd:01 543920     /usr/lib/libstdc++.so.6.0.19
4d38d000-4d394000 rw-p 00000000 00:00 0
b779f000-b77a2000 rw-p 00000000 00:00 0
b77b6000-b77b9000 rw-p 00000000 00:00 0
b77b9000-b77ba000 r-xp 00000000 00:00 0          [vdso]
bf7e1000-bf802000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

 

 

* Debugging

 

[root@localhost PracticeAlgorithm]# gdb a.out
GNU gdb (GDB) Fedora 7.6.1-46.fc19
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/broadcom/codes/PracticeAlgorithm/a.out...done.
(gdb) run
Starting program: /home/broadcom/codes/PracticeAlgorithm/a.out
4
spider man
*** Error in `/home/broadcom/codes/PracticeAlgorithm/a.out': double free or corruption (fasttop): 0x0804b008 ***
======= Backtrace: =========
/lib/libc.so.6[0x4c4e9b8a]
/lib/libstdc++.so.6(_ZdlPv+0x20)[0x4d2f2940]
/lib/libstdc++.so.6(_ZdaPv+0x1c)[0x4d2f299c]
/home/broadcom/codes/PracticeAlgorithm/a.out[0x80489d6]
/home/broadcom/codes/PracticeAlgorithm/a.out[0x80488ea]
/lib/libc.so.6(__libc_start_main+0xf3)[0x4c48d963]
/home/broadcom/codes/PracticeAlgorithm/a.out[0x80487a1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 fd:02 2762798    /home/broadcom/codes/PracticeAlgorithm/a.out
08049000-0804a000 r--p 00000000 fd:02 2762798    /home/broadcom/codes/PracticeAlgorithm/a.out
0804a000-0804b000 rw-p 00001000 fd:02 2762798    /home/broadcom/codes/PracticeAlgorithm/a.out
0804b000-0806c000 rw-p 00000000 00:00 0          [heap]
4c44c000-4c46b000 r-xp 00000000 fd:01 552980     /usr/lib/ld-2.17.so
4c46b000-4c46c000 r--p 0001e000 fd:01 552980     /usr/lib/ld-2.17.so
4c46c000-4c46d000 rw-p 0001f000 fd:01 552980     /usr/lib/ld-2.17.so
4c474000-4c62c000 r-xp 00000000 fd:01 552982     /usr/lib/libc-2.17.so
4c62c000-4c62e000 r--p 001b7000 fd:01 552982     /usr/lib/libc-2.17.so
4c62e000-4c62f000 rw-p 001b9000 fd:01 552982     /usr/lib/libc-2.17.so
4c62f000-4c632000 rw-p 00000000 00:00 0
4c670000-4c6b1000 r-xp 00000000 fd:01 552996     /usr/lib/libm-2.17.so
4c6b1000-4c6b2000 r--p 00040000 fd:01 552996     /usr/lib/libm-2.17.so
4c6b2000-4c6b3000 rw-p 00041000 fd:01 552996     /usr/lib/libm-2.17.so
4c899000-4c8b4000 r-xp 00000000 fd:01 552999     /usr/lib/libgcc_s-4.8.3-20140911.so.1
4c8b4000-4c8b5000 r--p 0001a000 fd:01 552999     /usr/lib/libgcc_s-4.8.3-20140911.so.1
4c8b5000-4c8b6000 rw-p 0001b000 fd:01 552999     /usr/lib/libgcc_s-4.8.3-20140911.so.1
4d2a7000-4d388000 r-xp 00000000 fd:01 543920     /usr/lib/libstdc++.so.6.0.19
4d388000-4d38c000 r--p 000e1000 fd:01 543920     /usr/lib/libstdc++.so.6.0.19
4d38c000-4d38d000 rw-p 000e5000 fd:01 543920     /usr/lib/libstdc++.so.6.0.19
4d38d000-4d394000 rw-p 00000000 00:00 0
b7fe5000-b7fe8000 rw-p 00000000 00:00 0
b7ffc000-b7fff000 rw-p 00000000 00:00 0
b7fff000-b8000000 r-xp 00000000 00:00 0          [vdso]
bffdf000-c0000000 rw-p 00000000 00:00 0          [stack]

Program received signal SIGABRT, Aborted.
0xb7fff424 in __kernel_vsyscall ()
Missing separate debuginfos, use: debuginfo-install glibc-2.17-21.fc19.i686 libgcc-4.8.3-7.fc19.i686 libstdc++-4.8.3-7.fc19.i686

 

// Backtrace

(gdb) bt full
#0  0xb7fff424 in __kernel_vsyscall ()
No symbol table info available.
#1  0x4c4a2687 in raise () from /lib/libc.so.6
No symbol table info available.
#2  0x4c4a3ec3 in abort () from /lib/libc.so.6
No symbol table info available.
#3  0x4c4e1f45 in __libc_message () from /lib/libc.so.6
No symbol table info available.
#4  0x4c4e9b8a in _int_free () from /lib/libc.so.6
No symbol table info available.
#5  0x4d2f2940 in operator delete(void*) () from /lib/libstdc++.so.6
No symbol table info available.
#6  0x4d2f299c in operator delete[](void*) () from /lib/libstdc++.so.6
No symbol table info available.
#7  0x080489d6 in myClass::~myClass (this=0xbffff198, __in_chrg=<optimized out>) at classbasic.cpp:27
No locals.
#8  0x080488ea in main (argc=1, argv=0xbffff244) at classbasic.cpp:41
        A = {a = 4, name = 0x804b008 ""}
        B = {a = 4, name = 0x804b008 ""}

 

(gdb) list
29   
30            void showData(){
31                cout << a <<endl;
32                cout << name << endl;
33            }
34   
35    };
36   
37    int main(int argc, char *argv[]){
38        myClass A(4,"spider man");
(gdb) list 28
23   
24            }
25    #endif
26            ~myClass(){
27                delete []name;
28            }
29   
30            void showData(){
31                cout << a <<endl;
32                cout << name << endl;
(gdb)


(gdb) print name
$1 = 0

 

(gdb) info locals
A = {a = 4, name = 0x804b008 ""}
B = {a = 4, name = 0x804b008 ""}

 

(gdb) info args
argc = 1
argv = 0xbffff244

(gdb) info stack
#0  0xb7fff424 in __kernel_vsyscall ()
#1  0x4c4a2687 in raise () from /lib/libc.so.6
#2  0x4c4a3ec3 in abort () from /lib/libc.so.6
#3  0x4c4e1f45 in __libc_message () from /lib/libc.so.6
#4  0x4c4e9b8a in _int_free () from /lib/libc.so.6
#5  0x4d2f2940 in operator delete(void*) () from /lib/libstdc++.so.6
#6  0x4d2f299c in operator delete[](void*) () from /lib/libstdc++.so.6
#7  0x080489d6 in myClass::~myClass (this=0xbffff198, __in_chrg=<optimized out>) at classbasic.cpp:27
#8  0x080488ea in main (argc=1, argv=0xbffff244) at classbasic.cpp:41


(gdb) info registers
eax            0x0    0
ecx            0x14da    5338
edx            0x6    6
ebx            0x4c62e000    1281548288
esp            0xbffff180    0xbffff180
ebp            0xbffff1a8    0xbffff1a8
esi            0x0    0
edi            0x0    0
eip            0x80488ea    0x80488ea <main(int, char**)+90>
eflags         0x246    [ PF ZF IF ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x33    51


(gdb) info frame
Stack level 8, frame at 0xbffff1b0:
 eip = 0x80488ea in main (classbasic.cpp:41); saved eip 0x4c48d963
 caller of frame at 0xbffff180
 source language c++.
 Arglist at 0xbffff1a8, args: argc=1, argv=0xbffff244
 Locals at 0xbffff1a8, Previous frame's sp is 0xbffff1b0
 Saved registers:
  ebx at 0xbffff1a4, ebp at 0xbffff1a8, eip at 0xbffff1ac

 

 

 

* Symbol table

 

[root@localhost PracticeAlgorithm]# nm a.out
0804a04c B __bss_start
0804a0ec b completed.5953
         U __cxa_atexit@@GLIBC_2.1.3
0804a048 D __data_start
0804a048 W data_start
080487c0 t deregister_tm_clones
08048830 t __do_global_dtors_aux
08049ef4 t __do_global_dtors_aux_fini_array_entry
08048ac0 R __dso_handle
08049efc d _DYNAMIC
0804a04c D _edata
0804a0f4 B _end
08048aa4 T _fini
08048ab8 R _fp_hw
08048860 t frame_dummy
08049eec t __frame_dummy_init_array_entry
08048c98 r __FRAME_END__
0804a000 d _GLOBAL_OFFSET_TABLE_
08048959 t _GLOBAL__sub_I_main
         w __gmon_start__
         U __gxx_personality_v0@@CXXABI_1.3
08048658 T _init
08049ef4 t __init_array_end
08049eec t __init_array_start
08048abc R _IO_stdin_used
         w _ITM_deregisterTMCloneTable
         w _ITM_registerTMCloneTable
08049ef8 d __JCR_END__
08049ef8 d __JCR_LIST__
         w _Jv_RegisterClasses
08048aa0 T __libc_csu_fini
08048a30 T __libc_csu_init
         U __libc_start_main@@GLIBC_2.0
08048890 T main
080487f0 t register_tm_clones
08048780 T _start
         U strcpy@@GLIBC_2.0
         U strlen@@GLIBC_2.0
0804a04c D __TMC_END__
         U _Unwind_Resume@@GCC_3.0
080487b0 T __x86.get_pc_thunk.bx
0804891a t _Z41__static_initialization_and_destruction_0ii
         U _ZdaPv@@GLIBCXX_3.4
080489d8 W _ZN7myClass8showDataEv
08048976 W _ZN7myClassC1EiPKc
08048976 W _ZN7myClassC2EiPKc
080489b8 W _ZN7myClassD1Ev
080489b8 W _ZN7myClassD2Ev
         U _Znaj@@GLIBCXX_3.4
         U _ZNSolsEi@@GLIBCXX_3.4
         U _ZNSolsEPFRSoS_E@@GLIBCXX_3.4
         U _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4
         U _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4
0804a060 B _ZSt4cout@@GLIBCXX_3.4
         U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4
0804a0f0 b _ZStL8__ioinit
         U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4

 

 

* References :

 

thrillfighter.tistory.com/146

http://scottmcpeak.com/memory-errors

 

반응형

+ Recent posts