|
-
December 8th, 2004, 12:11 PM
#1
Memory Leak
Hi
What is meant by memory leak and how can we prevent it.
Thanks in advance
Sudhakar.M
-
December 8th, 2004, 12:16 PM
#2
Re: Memory Leak
In short, a memory leak is experienced when a program allocates a block of memory and never frees it to the operating system.
A good way to prevent memory leaks is to avoid new/delete (or malloc/free) when writing C++ programs. Instead, use containers like std::vector, std::list, or std::map. Also, instead of using char * arrays to represent strings, use std::string.
-
December 8th, 2004, 12:44 PM
#3
Re: Memory Leak
Just an added note, it will be most obvious if you're allocating large blocks of data, or if you are allocating data in a loop and not freeing it. If you can't avoid new/delete, you can try using smart pointers. They have a couple of oddities, but they are very useful. Check it out here
http://ootips.org/yonat/4dev/smart-pointers.html
They have the added benefit of cleaning themselves up when they go out of scope. Good luck.
~Steve
-
December 8th, 2004, 12:59 PM
#4
Re: Memory Leak
Normally, you can throw exception to tell you that the memory is leaking!
-
December 8th, 2004, 05:56 PM
#5
-
December 8th, 2004, 11:51 PM
#6
Re: Memory Leak
The best way to know about a memory leak is overloading new and delete operators. In the new routine keep account of how much memory you have allocated and in delete routine keep account of memory that has been freed. So at the end you can flash a message showing the difference of total allocated and de-allocated memory. The difference is the MEMORY LEAK. It will show you how much bytes of memory is leaking. But it works with C++ only, not with C.
All the Best.
Mukesh Vijay
-
December 9th, 2004, 03:48 AM
#7
Re: Memory Leak
See also the FAQ about memory leaks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|