CS253: Software Development with C++

Spring 2018

Double Delete

See this page as a slide show

Double Delete

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): 0x0000000001b29c20 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81429)[0x7ff73e4ec429]
./a.out[0x400802]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7ff73e48d3d5]
./a.out[0x4006a9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:38 32391858                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
00600000-00601000 r--p 00000000 00:38 32391858                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
00601000-00602000 rw-p 00001000 00:38 32391858                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
01b18000-01b4a000 rw-p 00000000 00:00 0                                  [heap]
7ff738000000-7ff738021000 rw-p 00000000 00:00 0 
7ff738021000-7ff73c000000 ---p 00000000 00:00 0 
7ff73e46b000-7ff73e62e000 r-xp 00000000 08:03 1445204                    /usr/lib64/libc-2.17.so
7ff73e62e000-7ff73e82d000 ---p 001c3000 08:03 1445204                    /usr/lib64/libc-2.17.so
7ff73e82d000-7ff73e831000 r--p 001c2000 08:03 1445204                    /usr/lib64/libc-2.17.so
7ff73e831000-7ff73e833000 rw-p 001c6000 08:03 1445204                    /usr/lib64/libc-2.17.so
7ff73e833000-7ff73e838000 rw-p 00000000 00:00 0 
7ff73e838000-7ff73e84e000 r-xp 00000000 00:42 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7ff73e84e000-7ff73ea4d000 ---p 00016000 00:42 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7ff73ea4d000-7ff73ea4e000 rw-p 00015000 00:42 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7ff73ea4e000-7ff73eb4f000 r-xp 00000000 08:03 1456216                    /usr/lib64/libm-2.17.so
7ff73eb4f000-7ff73ed4e000 ---p 00101000 08:03 1456216                    /usr/lib64/libm-2.17.so
7ff73ed4e000-7ff73ed4f000 r--p 00100000 08:03 1456216                    /usr/lib64/libm-2.17.so
7ff73ed4f000-7ff73ed50000 rw-p 00101000 08:03 1456216                    /usr/lib64/libm-2.17.so
7ff73ed50000-7ff73eed4000 r-xp 00000000 00:42 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7ff73eed4000-7ff73f0d4000 ---p 00184000 00:42 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7ff73f0d4000-7ff73f0de000 r--p 00184000 00:42 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7ff73f0de000-7ff73f0e0000 rw-p 0018e000 00:42 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7ff73f0e0000-7ff73f0e4000 rw-p 00000000 00:00 0 
7ff73f0e4000-7ff73f106000 r-xp 00000000 08:03 1569143                    /usr/lib64/ld-2.17.so
7ff73f2d6000-7ff73f2db000 rw-p 00000000 00:00 0 
7ff73f303000-7ff73f305000 rw-p 00000000 00:00 0 
7ff73f305000-7ff73f306000 r--p 00021000 08:03 1569143                    /usr/lib64/ld-2.17.so
7ff73f306000-7ff73f307000 rw-p 00022000 08:03 1569143                    /usr/lib64/ld-2.17.so
7ff73f307000-7ff73f308000 rw-p 00000000 00:00 0 
7ffe2446c000-7ffe2448d000 rw-p 00000000 00:00 0                          [stack]
7ffe245db000-7ffe245dd000 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-01-07T11:12

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