How to create a memory leak in C++?
A memory leak occurs when you call new
without calling a corresponding delete
later. As illustrated in this sample code:
int main() {
// OK
int * p = new int;
delete p;
// Memory leak
int * q = new int;
// no delete
}
- Create pointer to object and allocate it on the heap
- Don't delete it.
- Repeat previous steps
- ????
- PROFIT