Am I freeing up the memory used in my program?

This is a discussion on Am I freeing up the memory used in my program? within the C++ Programming forums, part of the General Programming Boards category; Hi All, I've written a program for an assignment which utilises doubly-linked lists using the STL. I create a class ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    35

    Am I freeing up the memory used in my program?

    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!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    The clear() member function of std::list destroys the elements and sets the size of the container to 0. Although it may happen, especially since this is a doubly linked list, I do not think that you should be so sure that the memory will be released.

    Not to worry: when the container object goes out of scope, any remaining elements will be destroyed, and its associated memory will be released. Therefore, you do not need to do any special action to try and ensure that memory will be released.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Thanks for your quick response laserlight.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    You're welcome

    By the way, instead of:
    Code:
    while (!fin.eof())
    {
        fin.get(ch);
    write:
    Code:
    while (fin.get(ch)) 
    {
    The reason is that the EOF condition is only set after a read has failed to read more input. As a bonus, this simplifies your code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    While rare, it may be useful to clear the memory of a vector before it runs out of scope. You can, portably, like this:

    Code:
    vector<int> someVector;
    /* ...*/
    vector<int>().swap(someVector);
    That's the only way I know off to force the vector's memory to be cleared entirely.

    Don't use it, though, unless you have fairly good reason to do so. It's likely to only make your program slower significantly, while doing little to improve memory usage.

    Edit: Woops, it's a list. But the same holds for lists...
    Last edited by EVOEx; 05-04-2010 at 04:05 AM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Thanks!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    You could make a local scope to make sure the containers go out of scope before the function ends, too. But so long as you are using STL containers (unless you are working with pointers), all memory will be cleaned up correctly when they go out of scope. No need to worry.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  2. Program crashes and memory leaks
    By ulillillia in forum Tech Board
    Replies: 1
    Last Post: 05-15-2007, 11:54 PM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 03:38 PM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 04:13 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21