A memory leak is simply allocating memory and failing to release it when you're done. Technically, the following illustrates a memory leak:
#include <iostream>
using namespace std;
int main()
{
int *p = new int;
*p = 10;
cout<< *p << endl;
} In practice, the operating system will typically reclaim all memory allocated to a process when the process terminates, but that's not required by the language definition. However, long running programs can consume all fast memory and grind the program to a near halt, or crash the system if virtual memory is also exhausted. So any call to new must be paired with a call to delete:
#include <iostream>
using namespace std;
int main()
{
int *p = new int;
*p = 10;
cout<< *p << endl;
delete p;
} Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401