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): 0x0000000001447c20 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81429)[0x7f85926cd429]
./a.out[0x400752]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f859266e3d5]
./a.out[0x4005f9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:2e 20852160                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
00600000-00601000 r--p 00000000 00:2e 20852160                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
00601000-00602000 rw-p 00001000 00:2e 20852160                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
01436000-01468000 rw-p 00000000 00:00 0                                  [heap]
7f858c000000-7f858c021000 rw-p 00000000 00:00 0 
7f858c021000-7f8590000000 ---p 00000000 00:00 0 
7f859264c000-7f859280f000 r-xp 00000000 08:03 1445204                    /usr/lib64/libc-2.17.so
7f859280f000-7f8592a0e000 ---p 001c3000 08:03 1445204                    /usr/lib64/libc-2.17.so
7f8592a0e000-7f8592a12000 r--p 001c2000 08:03 1445204                    /usr/lib64/libc-2.17.so
7f8592a12000-7f8592a14000 rw-p 001c6000 08:03 1445204                    /usr/lib64/libc-2.17.so
7f8592a14000-7f8592a19000 rw-p 00000000 00:00 0 
7f8592a19000-7f8592a2f000 r-xp 00000000 00:40 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7f8592a2f000-7f8592c2e000 ---p 00016000 00:40 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7f8592c2e000-7f8592c2f000 rw-p 00015000 00:40 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7f8592c2f000-7f8592d30000 r-xp 00000000 08:03 1456216                    /usr/lib64/libm-2.17.so
7f8592d30000-7f8592f2f000 ---p 00101000 08:03 1456216                    /usr/lib64/libm-2.17.so
7f8592f2f000-7f8592f30000 r--p 00100000 08:03 1456216                    /usr/lib64/libm-2.17.so
7f8592f30000-7f8592f31000 rw-p 00101000 08:03 1456216                    /usr/lib64/libm-2.17.so
7f8592f31000-7f85930b5000 r-xp 00000000 00:40 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7f85930b5000-7f85932b5000 ---p 00184000 00:40 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7f85932b5000-7f85932bf000 r--p 00184000 00:40 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7f85932bf000-7f85932c1000 rw-p 0018e000 00:40 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7f85932c1000-7f85932c5000 rw-p 00000000 00:00 0 
7f85932c5000-7f85932e7000 r-xp 00000000 08:03 1569143                    /usr/lib64/ld-2.17.so
7f85934b7000-7f85934bc000 rw-p 00000000 00:00 0 
7f85934e4000-7f85934e6000 rw-p 00000000 00:00 0 
7f85934e6000-7f85934e7000 r--p 00021000 08:03 1569143                    /usr/lib64/ld-2.17.so
7f85934e7000-7f85934e8000 rw-p 00022000 08:03 1569143                    /usr/lib64/ld-2.17.so
7f85934e8000-7f85934e9000 rw-p 00000000 00:00 0 
7ffd191e2000-7ffd19203000 rw-p 00000000 00:00 0                          [stack]
7ffd193e6000-7ffd193e8000 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