| |
GeekInterview.com > Interview Questions > Programming > C++
| Print | |
Question: What is a memory leak? How can we avoid it?
|
| July 07, 2006 21:53:03 |
#2 |
| Sabith |
Member Since: Visitor Total Comments: N/A |
RE: What is a memory leak? How can we avoid it? |
A memory leak can be avoided by making sure that whatever memory has been dynamically allocated will be cleared after the use of the same. for example
int main()
{ char *myCharData[20];
for (int nLoop =0;nLoop < 20; ++nLoop) { myCharData[nLoop ] = new char[256];
strcpy(myCharData[nLoop],"SABITH");
.......
}
.........................
/*Some manipulations here using myCharData*/
/*Now here we have to clear the data. The place can vary according to ur program. This being a simple program,u can clear at the end*/
for(int nLoop =0;nLoop < 20; ++nLoop) {
delete[] myCharData[nLoop ];
}
return 0;
} |
| |
Back To Question | |