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): 0x0000000000ca3c20 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81429)[0x7f93a3160429]
./a.out[0x400752]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f93a31013d5]
./a.out[0x4005f9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:2c 20852160                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
00600000-00601000 r--p 00000000 00:2c 20852160                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
00601000-00602000 rw-p 00001000 00:2c 20852160                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
00c92000-00cc4000 rw-p 00000000 00:00 0                                  [heap]
7f939c000000-7f939c021000 rw-p 00000000 00:00 0 
7f939c021000-7f93a0000000 ---p 00000000 00:00 0 
7f93a30df000-7f93a32a2000 r-xp 00000000 08:03 1445204                    /usr/lib64/libc-2.17.so
7f93a32a2000-7f93a34a1000 ---p 001c3000 08:03 1445204                    /usr/lib64/libc-2.17.so
7f93a34a1000-7f93a34a5000 r--p 001c2000 08:03 1445204                    /usr/lib64/libc-2.17.so
7f93a34a5000-7f93a34a7000 rw-p 001c6000 08:03 1445204                    /usr/lib64/libc-2.17.so
7f93a34a7000-7f93a34ac000 rw-p 00000000 00:00 0 
7f93a34ac000-7f93a34c2000 r-xp 00000000 00:2e 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7f93a34c2000-7f93a36c1000 ---p 00016000 00:2e 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7f93a36c1000-7f93a36c2000 rw-p 00015000 00:2e 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7f93a36c2000-7f93a37c3000 r-xp 00000000 08:03 1456216                    /usr/lib64/libm-2.17.so
7f93a37c3000-7f93a39c2000 ---p 00101000 08:03 1456216                    /usr/lib64/libm-2.17.so
7f93a39c2000-7f93a39c3000 r--p 00100000 08:03 1456216                    /usr/lib64/libm-2.17.so
7f93a39c3000-7f93a39c4000 rw-p 00101000 08:03 1456216                    /usr/lib64/libm-2.17.so
7f93a39c4000-7f93a3b48000 r-xp 00000000 00:2e 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7f93a3b48000-7f93a3d48000 ---p 00184000 00:2e 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7f93a3d48000-7f93a3d52000 r--p 00184000 00:2e 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7f93a3d52000-7f93a3d54000 rw-p 0018e000 00:2e 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7f93a3d54000-7f93a3d58000 rw-p 00000000 00:00 0 
7f93a3d58000-7f93a3d7a000 r-xp 00000000 08:03 1569143                    /usr/lib64/ld-2.17.so
7f93a3f4a000-7f93a3f4f000 rw-p 00000000 00:00 0 
7f93a3f77000-7f93a3f79000 rw-p 00000000 00:00 0 
7f93a3f79000-7f93a3f7a000 r--p 00021000 08:03 1569143                    /usr/lib64/ld-2.17.so
7f93a3f7a000-7f93a3f7b000 rw-p 00022000 08:03 1569143                    /usr/lib64/ld-2.17.so
7f93a3f7b000-7f93a3f7c000 rw-p 00000000 00:00 0 
7ffd3fcf3000-7ffd3fd14000 rw-p 00000000 00:00 0                          [stack]
7ffd3fd8f000-7ffd3fd91000 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