LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 11-03-2004, 07:47 PM   #1
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
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?
 
Old 11-03-2004, 07:55 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 56
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.
 
Old 11-04-2004, 04:33 AM   #3
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 120Reputation: 120
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).
 
Old 11-04-2004, 09:41 PM   #4
karlan
Member
 
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Rep: Reputation: 30
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.
 
Old 11-05-2004, 02:54 AM   #5
aizkorri
Member
 
Registered: Feb 2002
Location: Basque Country
Distribution: Fedora 14, Ubuntu 11.10
Posts: 425
Blog Entries: 1

Rep: Reputation: 36
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.
 
Old 11-05-2004, 06:26 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 56
A great one for Linux is Valgrind.
 
Old 11-05-2004, 10:32 AM   #7
karlan
Member
 
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Rep: Reputation: 30
safe libc
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
memory leak ? os2 Programming 1 05-19-2005 01:45 PM
Memory Leak?! RoaCh Of DisCor Linux - General 9 05-07-2005 12:26 AM
memory leak mfitzpat Linux - Newbie 1 09-24-2004 02:58 PM
memory leak mdk Mandriva 1 09-17-2004 10:54 AM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM


All times are GMT -5. The time now is 08:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration