Exforsys

Online Training

Memory leaks in C program

This is a discussion on Memory leaks in C program within the C and C++ forums, part of the Programming Talk category; In my C program I use both "new" and "malloc" keywords, I want to know about ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-12-2006, 02:50 AM
Junior Member
 
Join Date: Jan 2006
Posts: 21
Charith is on a distinguished road
Memory leaks in C program

In my C program I use both "new" and "malloc" keywords, I want to know about memory leaks, What are the consideration when I use "new" and "malloc" keywords. Should I take any extra care to fix memory leaks? Please help me learn


Charith
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-14-2006, 06:37 AM
admin's Avatar
Administrator
 
Join Date: Mar 2005
Location: New York, USA
Posts: 309
admin has disabled reputation
Send a message via Yahoo to admin
There are various scenarios in which memory leaks can occur and taken care of in C program. I have specified some of them below:

Memory leaks can occur during a realloc. That is suppose if a programmer uses the same pointer as the pointer used in original memory allocation for the realloc pointer and if suppose the realloc fails then the pointer would be set to NULL which would make the previously allocated memory also having no pointer for it. This is one of the situations of memory leak. To handle this situation what one can do is use a temporary pointer for realloc so that even if realloc fails, it would not cause big hazard as the pointer on the original memory space is kept as such.

Since any memory space that loses its pointer or which is not free'd is called as memory leak it is always good to use free and the syntax of using this is

void free(void *p);

In the above free is a keyword which takes the pointer for freeing as argument.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-14-2006, 07:20 AM
Junior Member
 
Join Date: Jan 2006
Posts: 21
Charith is on a distinguished road
Hello Admin

Thanks a lot for posting answer to my question
I am very happy

Thanks again
Charith
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-13-2007, 10:34 PM
Senior Member
 
Join Date: Apr 2006
Posts: 144
caradoc is on a distinguished road
It was really a very useful post on Memory leaks. Thanks to the admin for such a wonderful posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-14-2007, 11:51 PM
Member
 
Join Date: May 2006
Posts: 38
zsk_00 is on a distinguished road
nice post, clears up some confusion
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-15-2004 07:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-01-2004 07:01 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 06-01-2004 07:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 05-15-2004 07:00 AM
Apple II Csa2 FAQs: Telecom Hardware & Transfers, Part 20/25 rubywand@swbell.net Tech FAQ 0 04-04-2004 08:29 AM


All times are GMT -4. The time now is 04:07 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.