I posted a program earlier today in response to another thread and noticed that I had forgotten to clean up/free the memory I used. Here's the program.
#include <iostream>
using namespace std;
int main()
{
int LIST_SIZE, NUM_CHARS_PER_ELEMENT;
cout << "Enter number of elements in list: ";
cin >> LIST_SIZE;
cout << "Enter number of characters in each array element: ";
cin >> NUM_CHARS_PER_ELEMENT;
char **list = new char*[LIST_SIZE];
for (int i = 0; i < LIST_SIZE; i++)
{
list[i] = new char[NUM_CHARS_PER_ELEMENT];
}
cout << "Enter " << LIST_SIZE << " names\n";
for (int i = 0; i < LIST_SIZE; i++)
{
cout << "Enter name " << i << " : ";
cin >> list[i];
}
cout << "Here are the names\n";
for (int i = 0; i < LIST_SIZE; i++)
{
cout << "Name " << i << " : " << list[i] << endl;
}
for (int i = 0; i < LIST_SIZE; i++)
delete []list[i];
delete list;
return 0;
}
I left off lines 33 through 36 in my prior post. My question is this. Are lines 33 - 36 necessary? What happens if I leave them out? Our instructors drilled into our heads the need to free any memory we reserved with new
once we were done with it, but in this case, since this is done at the very end of the program, does it really matter? Isn't the memory released automatically when the program ends?