GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 152 of 203    Print  
How will you detect if there is memory leak in your C++ program?

  
Total Answers and Comments: 6 Last Update: October 05, 2009     Asked by: rusty_man 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
November 08, 2007 03:20:37   #1  
Herojeet        

RE: How will you detect if there is memory leak in you...
Run "Rational Purify" it will show you all the leaks
 
Is this answer useful? Yes | No
November 26, 2007 10:12:29   #2  
abrankov Member Since: November 2007   Contribution: 1    

RE: How will you detect if there is memory leak in you...

Well "Rational Purify" is one way. If I was conducting the interview I would ask a contra question immediately asking how to do that if you do not have a port of the tool for the OS (Riviera Symbian etc.) you are using.
In this case you will need to find a way to precisely measure how much memory you have before and after you run the program. If there is a difference than you have a memory leak. How you do that vary from system to system. You will need to dig yourself in the documents for the system.


 
Is this answer useful? Yes | No
December 20, 2007 01:11:02   #3  
mukesh5sin Member Since: December 2007   Contribution: 1    

RE: How will you detect if there is memory leak in your C++ program?
If you are using new and delete operator you can overload both the operators and can count number off heap allocation and deallocation. You can also get the __FILE__ and __LINE__ to get to know the file name and line number. Now the new and delete of a memory location should be in pair and if its not there is a memroy leak. By using line and file utility you can reach upto the exact location.
 
Is this answer useful? Yes | No
December 07, 2008 09:41:00   #4  
burraganesh Member Since: December 2008   Contribution: 20    

RE: How will you detect if there is memory leak in your C++ program?
If memory cannot be allocated dynamically new throws bad_alloc such message will be displayed at execution.
If so then
Use cout statement in destructor to check if it was invoked (i.e. memory deallocated)

 
Is this answer useful? Yes | No
February 18, 2009 01:41:43   #5  
santhosh.kanchanapally Member Since: April 2008   Contribution: 10    

RE: How will you detect if there is memory leak in your C++ program?
By overloading the new and delete operators we can find the memory leaks as shown in following program

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int count 0;
void * operator new(size_t size)
{
count++;
cout<<"The size of memory allocated:"<<size<<"byes"<<"n";
return malloc(size);
};
void operator delete(void * mem)
{
count--;
free(mem);
};
int _tmain(int argc _TCHAR* argv[])
{
//int length sizeof("Santhosh");
char* Name (char*)operator new (sizeof("HelloWorld"));
strcpy_s(Name sizeof("HelloWorld") "HelloWorld");
operator delete(Name);
cout<<"The number of memory leaks:"<<count;
getchar();
}

 
Is this answer useful? Yes | No
October 04, 2009 12:37:38   #6  
vijay.badola Member Since: August 2008   Contribution: 2    

RE: How will you detect if there is memory leak in your C++ program?
In LINUX environment one can use VALGRIND and MPATROL free tools to find out possible memory leaks in programs.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape