CS253

CS253: Software Development with C++

Spring 2017

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;
free(): double free detected in tcache 2
SIGABRT: Aborted

Modified: 2017-01-06T15:30

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