Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-14-2007, 01:43 AM
c___newbie c___newbie is offline
Newbie
 
Join Date: Nov 2007
Posts: 1
Rep Power: 0
c___newbie is on a distinguished road
Default Memory leak prevention methodogies

Hi all,
I’m studying a problem releated to memory leakage.
Now, there are many tools to check memory leakake at run-time (dynamic analysis), and some tools can check memory leakage at compile-time (static analysis). But I have some questions:
How do you think to prevent memory leakage from coding phase (without using tool)? (or In coding phase, what do you have to do to prevent memory leakage?)
I want to investigate from many programmers in many countries.
Can you help me?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-17-2007, 08:48 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

The basic technique is to make sure that for every malloc() there is a corresponding free(). This takes care of the most obvious problem, but is far from error proof. For example, if you have code like the following:
Code:
int * ptr;
for (i=1;i<10;i++){ptr=malloc(int);}
free(ptr);
will leak memory like crazy because the malloc is inside the loop, but the free is outside the loop.
This type of situation can happen easily when dealing with data structures such as linked lists, where you'll have a pointer to the head of the list and the current node of the list, and each node has a pointer to the next node. When "deleting" the linked list you have to be very careful to free each node without losing access to the list in the process.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-18-2007, 11:29 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

I get lazy and use garbage collection. Let someone else worry about memory management .

As I see it. Unless I need programs that are always instantly responsive (i.e. 10ms pauses are unacceptable) then there is little or no reason not to use conservative GC. It does not produce an overly undue overhead in terms of processor time (because the majority of the deallocation code is only called once per thousands of deallocations while with manual memory management it's called once per deallocation). The only real issue is that GC can cause intense spots of activity where the program locks up. This is not usually noticeable in most applications.

A lot of C++ programmers use smart pointers to implement reference counting GC. This can work at a near manual level of efficiency. The only issue is circular references but it does generally stabilise memory usage. For me that is the key, I want to be able to tell people that this application uses N MiB of memory. I don't care that it wastes O MiB as long as it wastes O statically and no more than O.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hacking Applications With Memory Editors TcM Security Tutorials 8 06-07-2008 01:52 PM
Virtual Memory ... Windows XP mindsurfer Computer Hardware 1 11-01-2007 07:38 PM
Reading Memory locations on the fly and reacting to them Airthus C# Programming 0 10-01-2007 04:13 PM


All times are GMT -5. The time now is 02:03 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads