Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-03-2004, 07:47 PM
|
#1
|
Member
Registered: Jun 2004
Posts: 256
Rep:
|
what is memory leak
memory leak is a bug in program which cost program to not free memory, and it keep allocating memory until no more and program crash?
can someone give a really short simple program which have memory leak?
and show how to test it have memory leak?
|
|
|
11-03-2004, 07:55 PM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Code:
int main(void)
{
void *ptr;
for(;;)
ptr = malloc(1);
return 0;
}
There's no real way to test if there's a memory leak. You just have to look at the code or notice that your program just keeps taking more and more memory for no reason.
|
|
|
11-04-2004, 04:33 AM
|
#3
|
Senior Member
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Rep: 
|
A memory leak is technically when a pointer that has been allocated to a block of memory in the heap (also called global memory by some) has not been freed or the pointer variable has gone out of context without freeing the memory. In that case there is no way of referring to the allocated memory or freeing it. This is called a memory leak. The memory allocated will be locked off (unusable by other programs).
|
|
|
11-04-2004, 09:41 PM
|
#4
|
Member
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158
Rep:
|
all you need to know is:
1) if malloc/new(nothrow) returns NULL, memory allocation failed, pointer points nowhere.
2) for every successful new/malloc you should eventually call a delete/free
If you aren't going to do any class allocation programming in c++, use malloc and free, the will make your life easier. there is no free/free[] crap, like with c++ delete
Last edited by karlan; 11-04-2004 at 09:42 PM.
|
|
|
11-05-2004, 02:54 AM
|
#5
|
Member
Registered: Feb 2002
Location: Basque Country
Distribution: Fedora 14, Ubuntu 14.04
Posts: 434
Rep:
|
however, there are tools that check memory leaks in programs, I use them in a windows environment (rational purify) but there should be some for the LINUX environment.
|
|
|
11-05-2004, 06:26 AM
|
#6
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
A great one for Linux is Valgrind.
|
|
|
11-05-2004, 10:32 AM
|
#7
|
Member
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158
Rep:
|
safe libc
|
|
|
All times are GMT -5. The time now is 06:05 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|