“Typical”, I thought as I headed down to the interrogation room. I enter the dark decrepit crawlspace that the city has allocated for inquisition of criminals.
“Who do we have today?” I ask the guard on duty.
“We picked up this memory leak down the street,” came the reply, “He was acting suspicious, so we hauled him in for questioning.”
“I see. Give me the case file.”
I proceeded to enter the room. The single bulb fixture added to the already depressing mood as I sat down.
“Hello, I am Mr. Atchison, may I ask who you are?”
“My name is Leak, Memory Leak.”
“I see. Mr. Leak, do you know why you are here?”
“In fact I do. I was seen leaving the scene of a robbery.”
“So you confess then?”
“No, it wasn’t my fault, it was the other group members.”
“I believe you need to explain yourself Mr. Leak”
“Very well, it all began a few days ago…” He opens the case file:
class Car
{
public:
Car() {};
~Car() {};
void getAway() {};
};
int main()
{
//We decided to steal some cars from the local dealership
//in the beginning our plan was going so well
Car* stolenCar = new Car();
//We allocated the memory and everything
stolenCar->getAway();
//We got away and we thought that was it
//As you can see we leaked memory
}
“I could have sworn we made a clean get away.” He exclaims.
“Apparently not.” I reply. “Do you see where the memory leak was created?”
“Now I do,” he says. “If I only I called delete on our stolen Car, we could have avoided all of this mess.”
“You are absolutely right, but this isn’t the only incident is it?” I ask.
“No” He sighs and turns to the next page in the case file:
int main()
{
//After our supposed get away from the previous robbery, we decided to steal an array of cars
Car* carStorage = new Car[5];
//We allocated the memory and everything
//Learning from our past mistakes we even called delete to
//make sure the police could not follow us
//But i'm sure you knew we leaked memory again
delete carStorage;
return 0;
}
“Indeed. Do you know why?” I inquire.
“Not really” He replies.
“Well, when you allocate a pointer to an array of data you must use the [] operator to clean up after your pointer. Otherwise you only delete the first element and the rest of it is leaked.”
“That makes so much sense now” His face seems stuck in amazement. “I knew we leaked memory somehow.”
“I see the offense you got dragged in here on is next in the file.”
“It is.” He turns the page:
int main()
{
//What better way to hold all of our stolen goods, then in a vector?
//If only we had known...
vector<Car*> ourCars;
Car* temp; //our temporary storage device
//We allocated the memory and everything
for(int i = 0; i < 5; i++)
{
temp = new Car();
ourCars.push_back(temp);
}
//Learning from our past mistakes we even called delete to
//We hid out for a few days
//We need had to clean up after our vector
for(int i = 0; i < 5; i++)
{
ourCars.erase(i);//we thought this would take care of it
}
return 0;
}
“That is awful. You leaked all the memory in that vector.”
“I see that now, but I’m not entirely sure how we did that, I mean, we erased all the pointers.”
“You effectively erased all the handles to the allocated memory. It is now cut off from the system. Too much of that and the whole unit could crash.”
“I’m so sorry!” he sobbed.
“It’s worthless now! The damage is already done.”
“What is going to happen to me?”
“What people fail to realize is that memory leaks don’t spontaneously occur. They are the result from poorly designed code.”
“Wha…?”
“Therefore I will show you how to correctly allocate and free memory and you will be set free.”
“Oh thank you. Thank you.”
“Now look here” I open a folder on the desk:
Case 1:
class Car
{
public:
Car() {};
~Car() {};
void getAway() {};
};
int main()
{
//We decided to steal some cars from the local dealership
//in the beginning our plan was going so well
Car* stolenCar = new Car();
//We allocated the memory and everything
stolenCar->getAway();
//********* FREE your car***************
delete stolenCar;
return 0;
}
Case 2:
int main()
{
//After our supposed get away from the previous robbery, we decided to steal an array of cars
Car* carStorage = new Car[5];
//We allocated the memory and everything
//Learning from our past mistakes we even called delete to
//make sure the police could not follow us
//But i'm sure you knew we leaked memory again
//If you allocate an array of memory, delete an array of memory, use []
delete [] carStorage;
return 0;
}
Case 3:
int main()
{
//What better way to hold all of our stolen goods, then in a vector?
//If only we had known...
vector<Car*> ourCars;
vector<Car*>::iterator iter;//use an iterator
Car* temp; //our temporary storage device
//We allocated the memory and everything
for(int i = 0; i < 5; i++)
{
temp = new Car();
ourCars.push_back(temp);
}
//Free the allocated memory
for(iter = ourCars.begin(); iter != ourCars.end(); iter++)
{
delete (*iter);
}
ourCars.clear(); //gets rid of all the handles
return 0;
}
“Now that you know, I’m going to release you. But be careful when dealing with dynamic memory allocation/pointers. It is both C++’s greatest strength and greatest downfall for inexperienced programmers.”
“I will. I will. Thank you!” He gleefully leaves.
“I’m sure he’ll be back…”






MultiQuote












|