CS253: Software Development with C++

Spring 2018

Double Delete

See this page as a slide show

CS253 Double Delete

Double Delete

Every call to new must be matched by exactly one delete.

Not zero, and not two. One.

Similarly, every call to new [] must be matched by exactly one delete [].

Zero Delete

If you don’t call delete, then the memory is forgotten. We call this a memory leak.

Sure, the memory will be implicitly freed when the program ends. However, some programs run for a good long time before they end.

Multiple Delete

What happens if you call delete more than once?

float *p = new float[100];
delete[] p;
delete[] p;
*** Error in `./a.out': double free or corruption (top): 0x00000000016e8c20 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81489)[0x7fc470a5f489]
./a.out[0x400752]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fc470a003d5]
./a.out[0x4005f9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:2a 20852185                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6d2bbd8df440fc428b9c456a5102506d.exe
00600000-00601000 r--p 00000000 00:2a 20852185                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6d2bbd8df440fc428b9c456a5102506d.exe
00601000-00602000 rw-p 00001000 00:2a 20852185                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6d2bbd8df440fc428b9c456a5102506d.exe
016d7000-01709000 rw-p 00000000 00:00 0                                  [heap]
7fc46c000000-7fc46c021000 rw-p 00000000 00:00 0 
7fc46c021000-7fc470000000 ---p 00000000 00:00 0 
7fc4709de000-7fc470ba0000 r-xp 00000000 08:03 1445197                    /usr/lib64/libc-2.17.so
7fc470ba0000-7fc470da0000 ---p 001c2000 08:03 1445197                    /usr/lib64/libc-2.17.so
7fc470da0000-7fc470da4000 r--p 001c2000 08:03 1445197                    /usr/lib64/libc-2.17.so
7fc470da4000-7fc470da6000 rw-p 001c6000 08:03 1445197                    /usr/lib64/libc-2.17.so
7fc470da6000-7fc470dab000 rw-p 00000000 00:00 0 
7fc470dab000-7fc470dc1000 r-xp 00000000 00:2f 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7fc470dc1000-7fc470fc0000 ---p 00016000 00:2f 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7fc470fc0000-7fc470fc1000 rw-p 00015000 00:2f 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7fc470fc1000-7fc4710c2000 r-xp 00000000 08:03 1479561                    /usr/lib64/libm-2.17.so
7fc4710c2000-7fc4712c1000 ---p 00101000 08:03 1479561                    /usr/lib64/libm-2.17.so
7fc4712c1000-7fc4712c2000 r--p 00100000 08:03 1479561                    /usr/lib64/libm-2.17.so
7fc4712c2000-7fc4712c3000 rw-p 00101000 08:03 1479561                    /usr/lib64/libm-2.17.so
7fc4712c3000-7fc471447000 r-xp 00000000 00:2f 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7fc471447000-7fc471647000 ---p 00184000 00:2f 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7fc471647000-7fc471651000 r--p 00184000 00:2f 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7fc471651000-7fc471653000 rw-p 0018e000 00:2f 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7fc471653000-7fc471657000 rw-p 00000000 00:00 0 
7fc471657000-7fc471679000 r-xp 00000000 08:03 1444192                    /usr/lib64/ld-2.17.so
7fc471848000-7fc47184d000 rw-p 00000000 00:00 0 
7fc471876000-7fc471878000 rw-p 00000000 00:00 0 
7fc471878000-7fc471879000 r--p 00021000 08:03 1444192                    /usr/lib64/ld-2.17.so
7fc471879000-7fc47187a000 rw-p 00022000 08:03 1444192                    /usr/lib64/ld-2.17.so
7fc47187a000-7fc47187b000 rw-p 00000000 00:00 0 
7ffc252cc000-7ffc252ed000 rw-p 00000000 00:00 0                          [stack]
7ffc253b0000-7ffc253b2000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
SIGABRT: Aborted

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:51

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building