Could anyone define "Memory leaks" well ?
'cause pretty hard for me to understand with C++ books.

Recommended Answers

All 5 Replies

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;
}

Thank you for the answer.

the best bit is creating a loop of new arrays and watching the memory size increase :D

Hello Acidburn,

If you are going to do that, be sure to save your work first! I have done some expirements with linux processes, and was able to crash a computer in 20 seconds or so.

Christian

20 secs? may I have a look at the code? I'm running windows and it just fills up the memory very slowly ...took around 4 hrs! .. .:S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.