CS253

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): 0x00000000017e9c20 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7c619)[0x7feca5acf619]
./a.out[0x400802]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7feca5a74c05]
./a.out[0x4006a9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:41 20850406                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
00600000-00601000 r--p 00000000 00:41 20850406                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
00601000-00602000 rw-p 00001000 00:41 20850406                           /s/bach/a/class/cs253/public_html/Spring18/pmwiki/wiki.d/.live-code/6ad14a2c8d28c050420fc84da57da56a.exe
017d8000-0180a000 rw-p 00000000 00:00 0                                  [heap]
7feca0000000-7feca0021000 rw-p 00000000 00:00 0 
7feca0021000-7feca4000000 ---p 00000000 00:00 0 
7feca5a53000-7feca5c0b000 r-xp 00000000 08:03 1445210                    /usr/lib64/libc-2.17.so
7feca5c0b000-7feca5e0b000 ---p 001b8000 08:03 1445210                    /usr/lib64/libc-2.17.so
7feca5e0b000-7feca5e0f000 r--p 001b8000 08:03 1445210                    /usr/lib64/libc-2.17.so
7feca5e0f000-7feca5e11000 rw-p 001bc000 08:03 1445210                    /usr/lib64/libc-2.17.so
7feca5e11000-7feca5e16000 rw-p 00000000 00:00 0 
7feca5e16000-7feca5e2c000 r-xp 00000000 00:37 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7feca5e2c000-7feca602b000 ---p 00016000 00:37 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7feca602b000-7feca602c000 rw-p 00015000 00:37 25559686                   /usr/local/gcc-6.3.0/lib64/libgcc_s.so.1
7feca602c000-7feca612d000 r-xp 00000000 08:03 1457674                    /usr/lib64/libm-2.17.so
7feca612d000-7feca632c000 ---p 00101000 08:03 1457674                    /usr/lib64/libm-2.17.so
7feca632c000-7feca632d000 r--p 00100000 08:03 1457674                    /usr/lib64/libm-2.17.so
7feca632d000-7feca632e000 rw-p 00101000 08:03 1457674                    /usr/lib64/libm-2.17.so
7feca632e000-7feca64b2000 r-xp 00000000 00:37 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7feca64b2000-7feca66b2000 ---p 00184000 00:37 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7feca66b2000-7feca66bc000 r--p 00184000 00:37 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7feca66bc000-7feca66be000 rw-p 0018e000 00:37 25560479                   /usr/local/gcc-6.3.0/lib64/libstdc++.so.6.0.22
7feca66be000-7feca66c2000 rw-p 00000000 00:00 0 
7feca66c2000-7feca66e3000 r-xp 00000000 08:03 1444337                    /usr/lib64/ld-2.17.so
7feca68b5000-7feca68ba000 rw-p 00000000 00:00 0 
7feca68e1000-7feca68e3000 rw-p 00000000 00:00 0 
7feca68e3000-7feca68e4000 r--p 00021000 08:03 1444337                    /usr/lib64/ld-2.17.so
7feca68e4000-7feca68e5000 rw-p 00022000 08:03 1444337                    /usr/lib64/ld-2.17.so
7feca68e5000-7feca68e6000 rw-p 00000000 00:00 0 
7ffc59ce4000-7ffc59d05000 rw-p 00000000 00:00 0                          [stack]
7ffc59d29000-7ffc59d2b000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
SIGABRT: Aborted

Modified: 2018-01-07T11:12

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building