Hi All,
I've written a program for an assignment which utilises doubly-linked lists using the STL.
I create a class like so:
Code:
class row
{
private:
list<char> row_chars;
unsigned int row_number, char_position, row_size;
Which has a clear method like so:
Code:
void row::clear_row()
{
row_chars.clear();
}
And I create a list of objects of this class:
Code:
int main (int argc, char *argv[])
{
list<row> maze_rows;
So in the following code, I'm reading a line into a list of chars which is saved to a row object, then I add that row object to a list of row objects (maze_rows) and subsequently clear the list of chars and begin again.
Code:
void load_maze(list<row> &maze_rows, string arg)
{
string filein;
row row_object(1);
ifstream fin;
char ch;
filein = arg;
//open stream to filein
fin.open(filein.c_str());
if (!fin)
{
cerr << "Unable to load maze " << filein << " \n";
exit(0);
}
while (!fin.eof())
{
fin.get(ch);
row_object.store_chars(ch);
list<row>::iterator itr = maze_rows.begin();
if(ch == '\n')
{
maze_rows.push_back(row_object);
row_object.clear_row();
row_object.new_row();
}
}
fin.close();
At the end of main I include the following code:
Code:
//Cleanup
clear_rows(maze_rows);
return 0;
}
Which runs:
Code:
void clear_rows(list<row> &maze_rows)
{
for(list<row>::iterator list_iter = maze_rows.begin();
list_iter != maze_rows.end(); list_iter++)
{
list_iter->clear_row();
}
}
What I want to know is, is this effectively freeing up any memory I had been using for these lists? My grasp on memory management is a bit hazy because I've only ever programmed in Java before and it wasn't that necessary.
Thanks in advance for all your help!