944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3305
  • C++ RSS
Mar 23rd, 2005
0

The definiton of "Memory leaks"

Expand Post »
Could anyone define "Memory leaks" well ?
'cause pretty hard for me to understand with C++ books.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tenoran is offline Offline
9 posts
since Mar 2005
Mar 23rd, 2005
0

Re: The definiton of "Memory leaks"

A memory leak is simply allocating memory and failing to release it when you're done. Technically, the following illustrates a memory leak:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int *p = new int;
  8.  
  9. *p = 10;
  10.  
  11. cout<< *p << endl;
  12. }
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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int *p = new int;
  8.  
  9. *p = 10;
  10.  
  11. cout<< *p << endl;
  12.  
  13. delete p;
  14. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 28th, 2005
0

Re: The definiton of "Memory leaks"

Thank you for the answer.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tenoran is offline Offline
9 posts
since Mar 2005
Mar 28th, 2005
0

Re: The definiton of "Memory leaks"

the best bit is creating a loop of new arrays and watching the memory size increase
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Mar 28th, 2005
0

Re: The definiton of "Memory leaks"

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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Mar 28th, 2005
0

Re: The definiton of "Memory leaks"

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! .. .
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Is there an array[x][y][z] in C++ (i.e TRIPLE ARRAY)???
Next Thread in C++ Forum Timeline: OOp memory leak error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC